IE JavaScript cookie path setting bug

Source: Internet
Author: User

a cookie was set up in the project and was successfully tested under Firefox. There was a problem with the IE test, and the location found to be a problem with the path to JavaScript setting cookies. The cookie under IE is set to the path of the URL, and if the cookie value is scoped to the current URL, then the JavaScript under IE cannot get the cookie value set.

Look at the following demo code, note the following path differences :

  1. var cookie_name = "name";
  2. var cookie_value = "value";
  3. expires = new Date ();
  4. Expires.settime (Expires.gettime () + 86400 * 1000);
  5. // bug Document.cookie cannot read to Cookie_name value
  6. Document.cookie = Cookie_name + "=" + encodeuricomponent (cookie_value)
  7. + "; expires= " + expires.togmtstring () + "; path="
  8. + Window.location.pathname;
  9. //Normal
  10. Document.cookie = Cookie_name + "=" + encodeuricomponent (cookie_value)
  11. + "; expires= " + expires.togmtstring () + "; path=/test/";
  12. //Normal
  13. Document.cookie = "cookie_name=" + encodeuricomponent (cookie_value)
  14. + "; expires= " + expires.togmtstring () + "; path=/";

Results: The cookie value is only valid for the current page. I don't know now.

Alternative, Code:

  1. //ie Cookie Bug substitution scheme
  2. var cookie_path = Window.location.pathname;
  3. var cookie_name = encodeURIComponent (cookie_path.substring (Cookie_path
  4. . LastIndexOf ('/') + 1));
  5. Cookie_path = cookie_path.substring (0, Cookie_path.lastindexof ('/') + 1);
  6. var cookie_value = "value";
  7. expires = new Date ();
  8. Expires.settime (Expires.gettime () + 86400 * 1000);
  9. Document.cookie = Cookie_name + "=" + encodeuricomponent (cookie_value)
  10. + "; expires= " + expires.togmtstring () + "; Path= " + Cookie_path;

The alternative is to make a cookie name based on the URL file name of each page, valid within the current page path.
such as: URL equals/test/test.html
The cookie is named test.html and the valid path is/test/
This can also be done with the same JS deployment on each page can read to the current URL only valid cookie value. The price is that when users access many pages in the same level directory, the cookie value increases. As a result, each HTTP request under the current path has a long cookie, which directly causes the server to receive the header length of the client request, and the server burden increases as the traffic grows. And according to the definition of RFC 2109 cookie also has the length and number of limits, ie the maximum allowable cookie length is 4096 bytes, allow 50 cookie name-value pairs. If you want to break the limit of 50 name-value pairs, you can save more cookie variables in a single name-value using a cookie dictionary.

Reference from: http://conkeyn.iteye.com/blog/423549



IE JavaScript cookie path setting bug

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.