Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe to create cookie before document ready?

I'm currently saving a cookie in jQuery's document ready event handler, like:

$(function() {
    document.cookie = <cookie with info not dependent on DOM> 
});

Is it possible and safe to save a cookie even earlier, e.g. as a JavaScript statement outside any event handler that executes as the JavaScript file is being interpreted? Any browsers that may not be reliable to do in?

like image 239
Ben Amada Avatar asked Nov 12 '22 03:11

Ben Amada


1 Answers

It is 100% ok to read and write to cookies before the DOM has completed loading if you are not dependent on values from the DOM. If you use the Ghostery extension for Chrome and go to any website you can have a look at the tracking tags that load before the DOM is ready, most of which will be using normal cookies and that will give you an idea of how common it is to do this.

like image 128
James Dewes Avatar answered Nov 14 '22 23:11

James Dewes