Home > database >  Why am I not able to set cookies on my device?
Why am I not able to set cookies on my device?

Time:06-07

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!

CodePudding user response:

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

''
  • Related