javascript can't get/set cookie in ie7
I have to store, in 2 cookie开发者_JS百科 dictionary/keyed cookie, a user's preference for hiding certain sections of each form on the app.
It would appear that IE7 has reached some limit for the number or size of the cookie in javascript.
I mean that the cookies are only inaccessible on the client-side js, but can be read just fine in our asp.net/vb code-behind.
The functions that need to read the value of these cookies must be on client side because I can't run a postback everytime the function needs to update the cookie.
How should I go about getting and setting the cookie without triggering postback? I thought about storing the value of the cookie onload in a textbox, but I don't know when I would read it back and update the cookie.
Replacing answer because the previous one didn't apply and the comments were getting prettly long.
You are limited to a grand total of 4KB of cookie data per domain depending on browser.
RFC states that browsers need to support at least 20 cookies (IE does 50), and they need to support at least 4KB.
However, the sad truth is that most browsers took the 4KB to be the upper bound. So for an application that might be used "in the wild", the sum total of all the data across those cookies should not exceed 4KB.
Also, 4KB is apparently subjective as some browsers took this to mean 4095 bytes. Odd one that.
Anyway, current versions of Chrome and Firefox do not have this upper boundary. IE 9 caps at 10,234 bytes per domain, 5,117 per cookie (another odd number); however, Opera and Safari are still in the old days. Then again, if you are testing older versions of IE, Firefox or even Chrome then you'll run into the 4KB limit as well.
Personally, I think that if your cookies are greater than 4KB then you probably have some design issues as you are now putting more pressure on the tiniest connection you have: between the browser and your server. If you need more data than that then you might want to set up a local caching server, embed the data in the page or, at the very least, reconsider what you are using cookies for.
An interesting link: http://browsercookielimits.x64.me
As far as setting and reading them, check out the jQuery plugin for dealing with cookies:
http://plugins.jquery.com/project/Cookie
If you use the plugin to set them, they'll be readable.
精彩评论