I am under the impression that document.cookie = "mySessionCookie=mySessionToken"
does not get set right away in the browser.
More precisely, here's my situation:
document.cookie = "mySessionCookie=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT"
.window.location.reload()
to confirm that I have been logged out.It mostly works, but, quite often, it appears that the browser does not have enough time to set my new cookie value before calling window.location.reload()
.
Unless I am doing something wrong in my code, that behaviour suggests that document.cookie = "value"
is not executed on the same stack (so, it might not be asynchronous by itself, but it has an unpredictable behaviour when used with the rest of the code).
So, my question is, could document.cookie = "value"
be executed on a different stack?
The document. cookie interface is synchronous, single-threaded, and blocking.
Cookies are set using the Set-Cookie header field, sent in an HTTP response from the web server. This header field instructs the web browser to store the cookie and send it back in future requests to the server (the browser will ignore this header field if it does not support cookies or has disabled cookies).
The document. cookie attribute simply returns a string containing a semicolon and a space separated list of all cookies (i.e. name=value pairs, for example, firstName=Fabulous; lastName=Designs; ). This string does not include any of the cookie's characteristics, such as expires, path, domain, and so on.
Cookies are small pieces of text sent to your browser by a website you visit. They help that website remember information about your visit, which can both make it easier to visit the site again and make the site more useful to you.
Indeed, as pointed out by @charlietfl as a comment to my original question, the document.cookie
setter will set the cookie right away.
My real problem was that I had an error in my program, where I was not paying attention to the fact that browsers will set the cookie's "path" based on the current URI when no path is provided along with the cookie value when setting a cookie using JavaScript.
For example, if one wants to clear a session for all URIs at example.com
while viewing a resource at http://example.com/fake-resource
, one must explicitly write:
document.cookie = "sessionToken=;Path=/;Expires=Thu, 01 Jan 1970 00:00:00 GMT"
else (i.e. if Path=/ is omitted) the browser will create a second cookie for example.com/fake-resource
, such that the main cookie will continue to exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With