0

I copied and used getCookie and setCookie from W3Schools(http://www.w3schools.com/js/js_cookies.asp). Here are the codes of get and set:

  function setCookie(c_name,value,exdays)
  {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
  }

  function getCookie(c_name)
  {
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1)
    {
      c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1)
    {
      c_value = null;
    }
    else
    {
      c_start = c_value.indexOf("=", c_start) + 1;
      var c_end = c_value.indexOf(";", c_start);
   if (c_end == -1)
   {
      c_end = c_value.length;
   }
      c_value = unescape(c_value.substring(c_start,c_end));
   }
      return c_value;
 }

I frist set cookie in prepareDrive.html page

 setCookie("pathName",path,365);
 setCookie("formatName",ifFormat,365);

Then I called get cookie in startInstall.html page which is a different HTML page

 var path = getCookie("pathName");
 var ifFormat = getCookie("formatName");

but both path and ifFormat are null. However, when I console.log in prepareDrive.html, the data is there. Thanks !!! This is my first time to use cookie in JS. I do not want to use localstorage to store data.Because some old version browsers do not support this function ,right?

1
  • 1
    Here's a tip. W3Schools is a pretty horrible resource to learn from (w3fools.com) . There are plenty of questions here on SO that explain how to handle cookies, as well as a decent tutorial on MDN (Mozilla Developer Network) and a bunch of decent libraries that work (jQuery cookies if you're into that sort of thing, or Cookies.js which I just used today and was nice). W3Schools are just really good with SEO. Commented Jul 23, 2013 at 17:41

2 Answers 2

3

You need to specify a common path for the cookie. Simplest is just to specify the domain root:

var c_value=escape(value) + "; path=/" + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());

Without that, document.cookie will default to the current location.pathname, making the cookie only available to the current page.

;path=path (e.g., '/', '/mydir') If not specified, defaults to the current path of the current document location.


Also, I suggest having a look at the "little framework" for cookies on MDN.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but after I added path, it still shows null. And even show null in current html page. Here is how I check the error, maybe I am wrong. After setCookie("pathName",path,365); I called console.log("get cookie information = " + getCookie("pathName")); immediately. And it printed out null.
0

If you are already using jquery you might want to consider the jquery plugin:

https://github.com/jquery/plugins.jquery.com

That makes it pretty straightforward.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.