Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Document.cookie's setter asynchronous in web browsers?

Tags:

javascript

dom

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:

  • I am logged in.
  • I want to logout using Javascript.
  • So I set, for instance document.cookie = "mySessionCookie=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT".
  • Then I call 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?

like image 911
focorner Avatar asked Jun 11 '16 13:06

focorner


People also ask

Are document cookies synchronous?

The document. cookie interface is synchronous, single-threaded, and blocking.

How are cookies set in the browser?

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).

How does document cookie work?

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.

What are cookies in websites?

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.


1 Answers

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.

like image 68
focorner Avatar answered Sep 30 '22 11:09

focorner