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

1

I have following code:

In the file 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 LendoCookie() function to work before I have to click Create Cookie but just because I set an expiration time of cookie short of 2 minutes, funçãoLendoCookie() returns the cookie key when opening the cookieSimplescont2.html file using IE or Firefox, but not in chrome. I've already been to the Chrome cookie settings and in the privacy field I chose the "Allow local data configuration (Recommended)" option. but even so, it does not return any value, can someone tell me why?

    
asked by anonymous 06.02.2017 / 20:44

2 answers

1
The document.cookie does not work in the file:/// protocol and often does not work if the domain accessed is http://localhost and sometimes neither http://127.0.0.1 accepts unless they are set via header, which usually can only be done via the back-end.

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

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

  • http://127.0.0.1
  • If the first one does not 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 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 example Linux).

    Windows

  • Open notepad.exe (Notepad) as administrator, right-click select execute as administrator
  • Temporarily disable the antivirus
  • No notepad.exe click:

    • en-us: File > Open ...
    • en-US: File > Open ...
    • in: File > Open ...
  • The window will appear to navigate and choose the file, paste in the address field: %windir%\System32\drivers\etc\hosts

  • 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
    
  • Save the document and reactivate the antivirus

  • Restart your HTTP server (Apache, etc)
  • Type in the address bar of your browser this link
  • Mac OSX

  • Open Terminal
  • Type (I'm not sure which one is correct):

    sudo nano /etc/hosts
    

    or    sudo nano / private / etc / hosts

  • Go to the last line by nano and add something like this:

    127.0.0.1       meusite.local
    
  • Ctrl to save (Not Cmd is Ctrl ))

  • Press Ctrl + X to exit
  • Restart the HTTP server (Apache, etc)
  • Type in the address bar of your browser this link
  • Conclusion

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

        
    07.02.2017 / 15:51
    1

    I think it might actually be some local configuration. 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 JsFiddle link.

    Here with me it worked perfectly.

        
    06.02.2017 / 21:41