1

I am not able to set javascript cookies on my device for some reason. I am using this code:

<!DOCTYPE html>  
<html>  
<head>  
</head>  
<body>  
<input type="button" value="setCookie" onclick="setCookie()">  
<input type="button" value="getCookie" onclick="getCookie()">  
    <script>  
    function setCookie()  
    {  
        document.cookie="username=Duke Martin";  
    }  
    function getCookie()  
    {  
        if(document.cookie.length!=0)  
        {  
        alert(document.cookie);  
        }  
        else  
        {  
        alert("Cookie not available");  
        }  
    }  
    </script>  
  
</body>  
</html>

I have tried setting different cookie codes from different websites on different browsers , but none of them work. The thing is that even codes from w3schools which set cookies on my friend's device are not able to set cookies on my device .Could you please tell me if there is some sort of debugger so that I can make cookies work? P.S.: I have tried over 15 cookie codes in the past 3 hours but I am not able to set any cookies! Thanks in advance!

KrotAsch
  • 9
  • 5

1 Answers1

2

Check if your browser has local cookies support turned on!

If you are using your browser in incognito mode:

In Incognito, none of your browsing history, cookies and site data, or information entered in forms are saved on your device.

If you are using Chrome:

Chrome ignores cookies from local pages. see: Cannot set cookies in Javascript

What do you get if you try to read out your set cookie in the console?

document.cookie = "username=John Doe";
document.cookie;

My result in Mozilla Firefox Version 101.0

'username=John Doe'

My result in Chrome Version 102.0.5005.63

''
Jonathan
  • 131
  • 3
  • 2
    Cookies work in Incognito mode, they just go away at the end of the session. – ceejayoz Jun 06 '22 at 12:44
  • I am not using incognito mode . And I got your point as well . But I want to turn the html code into an app using electron.js in the future , so would it still show the same issue? Thanks! – KrotAsch Jun 06 '22 at 12:45
  • Electron.js has a standard usage for it. see: [https://electronjs.org/docs/api/cookies](https://www.electronjs.org/docs/latest/api/cookies). I think using the standard is the best way to go but unfortunately I can't say more about it. – Jonathan Jun 06 '22 at 12:56