Why session cookie? cannot be generated with js

in chrome, run

<html>
<body>
 
<button id="cookie">test</button>
 <script>
function setCookie()
{
  document.cookie = "flag=1;expires="";path=/";
}
ob = document.getElementById("cookie");
ob.addEventListener("click",setCookie);
</script>
</body>
</html>
After the

web page is opened, cookie is generated, but after closing the web page and reopening it, the cookie is still there, not a session cookie, excuse me, how to use js to generate session cookie?

document.cookie = "flag=1;expires=0;path=/"; can"t do it

Dec.10,2021

you can create an expires as long as it is left blank. I tried to turn off the browser on my computer to remove cookie. Whether you just close the tab, only closing the tab can not remove cookie.

A better way to write:
document.cookie = "cookiename=value; expires=0; path=/";

the picture is to test your code, pay attention to the expiration time, you can see that it is correct.


whether you just closed the tab, only closing the tab can not remove cookie.
judge correctly

Menu