I can’t read the result of my cookie in the Chrome Browser, what to do?

Asked

Viewed 736 times

1

I have following code:

In the archive cookieSimplescont2.js:

function criarCookie(valorCookie){
 //Criar objeto data
 var data = new Date();

 // setando o tempo de vida do cookie
 data.setTime(data.getTime()+120000);

 //Criando a estrutura do Cookie
 document.cookie = "primeiroCookie="+valorCookie+" ; expires="+data.toUTCString()+" ; path=/";

 return "Sucesso";

}

function lendoCookie(){
    return document.cookie;
}
<!DOCTYPE html>
<html>
<head>
    <title>Cookies</title>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="css/estilo.css">
    <script type="text/javascript" src="js/cookieSimplescont2.js"></script>
</head>
<body>
<h1>HTML SO PARA TESTES--- Testando Cookies</h1>

 <button onclick="alert(criarCookie('DiaBonito'))">Criar Cookie</button>
 <button onclick="alert(lendoCookie())">Lendo Cookie</button>

</body>
</html>

Well, for the job LendoCookie() work before I have to click on Cookie but only because I set a spiraling time of the cookie 2 minutes short, the funçãoLendoCookie() returns the key of cookie when I open the file cookieSimplescont2.html using IE or Firefox, but not Chrome. I already went to Chrome’s cookie settings and in the privacy field I chose "Allow local data configuration (recommended)." but even then does not return any value, someone can tell me why ?

  • Are you using a web server? type wamp or xampp?

  • @Guilherme Nascimento no

  • @Mandy then cookies will not work as I explained here https://answall.com/a/182596/3635

2 answers

1


The document.cookie do not work in protocol file:/// and often do not work if the domain accessed is http://localhost and sometimes even the http://127.0.0.1 accepted unless set via "header", which can usually only be done via back-end.

To avoid this you can try to install something like Apache, Nginx, Lightttpd or IIS or even use local servers for development (as soon as you can I put some examples).

After installing the "http" server on your machine just access to play your . html and . js on htdocs (usually apache) or if you already have an http server, then just try the following addresses:

  1. http://127.0.0.1
  2. If the first one doesn’t work try the local IP of your machine, for example something similar to http://192.168.1.55 or similar to http://10.0.0.55

Still using dynamic local Ips can be a huge problem, since they usually change, so you can create a "pseudo host" on your machine, just edit the file hosts (if it is Windows, then edit with a Linux example).

Windows

  1. Open the notepad.exe (Notepad) as administrator by clicking as mouse right select Run as administrator
  2. Disable the antivirus temporarily
  3. In the notepad.exe click on:

    • en: Filing cabinet > Open up...
    • pt-PT: File > Open up...
    • en: File > Open...
  4. The window will appear to browse and choose the file, paste in the field this address: %windir%\System32\drivers\etc\hosts

  5. After the line: # localhost name resolution is handled within dns itself. add this:

    # localhost name resolution is handled within dns itself.
        127.0.0.1       localhost
        ::1             localhost
        127.0.0.1       meusite.local #adicione esta linha
    
  6. Save the document and reactivate the antivirus

  7. Restart your HTTP server (Apache, etc)
  8. Type in the address bar of your browser this http://meusite.place/

Mac OSX

  1. Open the Terminal
  2. Type (not sure which is correct):

    sudo nano /etc/hosts
    

    or sudo nano /private/etc/hosts

  3. Go to the last line by nano and add something like that:

    127.0.0.1       meusite.local
    
  4. Squeeze Ctrl+The to save (It is not Cmd is Ctrl even)

  5. Squeeze Ctrl+X to get out
  6. Restart the HTTP server (Apache, etc)
  7. Type in the address bar of your browser this http://meusite.place/

Completion

This Host will only be accessible on your local machine and will allow you to "simulate" access to http://localhost as if you were accessing any other website through the pseudo-domain meusite.local, thus avoid problems when setting the document.cookie in Chrome and Safari

1

I believe it might actually be some local configuration. By putting your example in jsFiddle it works correctly.

function criarCookie(valorCookie) {
  //Criar objeto data
  var data = new Date();

  // setando o tempo de vida do cookie
  data.setTime(data.getTime() + 120000);

  //Criando a estrutura do Cookie
  document.cookie = "primeiroCookie=" + valorCookie + " ; expires=" + data.toUTCString() + " ; path=/";

  return "Sucesso";

}

function lendoCookie() {
  return document.cookie;
}
<h1>HTML SO PARA TESTES--- Testando Cookies</h1>

<button onclick="alert(criarCookie('DiaBonito'))">Criar Cookie</button>
<button onclick="alert(lendoCookie())">Lendo Cookie</button>

Follow the link from Jsfiddle.

Here with me it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.