saving cookies in Javascript -


hey trying save cookie , load/read again on reload. whenever reload error message mycookie undefined. can me out.

var mycookie  window.onload = function () {    console.log(mycookie);   var = document.getelementbyid("style1");   if (mycookie == undefined) {     console.log("new");   var mycookie = document.cookie = "common.css";     } else {     console.log("old");   }    a.href = mycookie; }  function change() {      var = document.getelementbyid("style1");     if(mycookie=="common.css")     {       mycookie  = document.cookie = "common2.css";     }else{       mycookie  = document.cookie = "common.css";     }     a.href = mycookie; } 

you need read more javascript cookies. using unnecessary variable you're declaring again on local scope.

when window loads uninitialised variable such

var mycookie; 

will undefined. should start initialling document.cookie

var mycookie = document.cookie; 

now, set cookie later use, need key like:

document.cookie = "mycookie=myvalue"; 

you can read more here: http://www.w3schools.com/js/js_cookies.asp


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -