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 :
- var cookie_name = "name";
- var cookie_value = "value";
- expires = new Date ();
- Expires.settime (Expires.gettime () + 86400 * 1000);
- // bug Document.cookie cannot read to Cookie_name value
- Document.cookie = Cookie_name + "=" + encodeuricomponent (cookie_value)
- + "; expires= " + expires.togmtstring () + "; path="
- + Window.location.pathname;
- //Normal
- Document.cookie = Cookie_name + "=" + encodeuricomponent (cookie_value)
- + "; expires= " + expires.togmtstring () + "; path=/test/";
- //Normal
- Document.cookie = "cookie_name=" + encodeuricomponent (cookie_value)
- + "; expires= " + expires.togmtstring () + "; path=/";
Results: The cookie value is only valid for the current page. I don't know now.
Alternative, Code:
- //ie Cookie Bug substitution scheme
- var cookie_path = Window.location.pathname;
- var cookie_name = encodeURIComponent (cookie_path.substring (Cookie_path
- . LastIndexOf ('/') + 1));
- Cookie_path = cookie_path.substring (0, Cookie_path.lastindexof ('/') + 1);
- var cookie_value = "value";
- expires = new Date ();
- Expires.settime (Expires.gettime () + 86400 * 1000);
- Document.cookie = Cookie_name + "=" + encodeuricomponent (cookie_value)
- + "; 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