View Full Version : Setting a cookie using document.cookie with IE7
I have an embedded web system where I would like to have the client PC retain page settings. Everything works great on Firefox (FF), but IE7 is problematic.
IE7 is not saving the cookie, while FF does.
Is there a way to get IE7 to allow cookies set by javascript or will I need to modify the embedded device to generate the cookie?
My google searches came up short for the solution to keeping this client side.
-Rob
jscheuer1
12-04-2007, 05:16 PM
Client side cookies are unreliable in all browsers. They depend upon the browser settings, and upon users not getting rid of them, and/or editing them. They are fine for things that aren't issues of security, or commerce, or otherwise mission critical - even so, if the client is refusing them for whatever reason, your page should designed so as to still be able to be accessed and used without any major problems.
However, it sounds to me as though you might possibly just not be using effective client side cookie code. I really like Quirksmode.org's cookie code:
http://www.quirksmode.org/js/cookies.html
It isn't perfect (at least according to some folks I've gone over it with), but it has never failed me in extensive application.
The one other possibility I can think of is that you might not have the cookie logic really well thought out visa vis what you are telling the cookies to be and when, how and when you are evaluating their values. Different browsers sometimes see the state of affairs that you are cuing these actions off of differently, if that is the problem, it must be taken into account in your logic.
The site works fine without cookies, but I would like to retain a user setting for some radio buttons. Currently there are 3 choices (A_radio, B_radio, C_radio). By default, A_radio is selected.
If the cookie does not exist, then A_radio is used. If the cookies does exist, select the appropriate radio button.
Everything works perfectly on FireFox. IE7 is not generating the cookie. I have gone over numerous IE7 settings, but still no luck.
Ultimately, browser setting requirements are fine because we will include it in our product specifications. If I cannot get the client side code to work, I may have to try to develop the cookie into our embedded device (a relatively large task).
I am using the very code from Quirksmode.org. I call "SetCookie("units", "Raw", 365);" the cookie stores fine on FF, but not on IE.
SetCookie is called whenever the radio button changes. During the initialization routine of a JavaScript, readCookie is used.
I believe SetCookie is not working because I do not see a cookie in my "Cookies" folder for IE7. I have purged the folder before and after each attempt.
I did run into problems with an alternate method to readCookie, GetCookie, but that was a simple boundary condition.
Any other thoughts?
Thanks,
Rob
Client side cookies are unreliable in all browsers. They depend upon the browser settings, and upon users not getting rid of them, and/or editing them. They are fine for things that aren't issues of security, or commerce, or otherwise mission critical - even so, if the client is refusing them for whatever reason, your page should designed so as to still be able to be accessed and used without any major problems.
However, it sounds to me as though you might possibly just not be using effective client side cookie code. I really like Quirksmode.org's cookie code:
http://www.quirksmode.org/js/cookies.html
It isn't perfect (at least according to some folks I've gone over it with), but it has never failed me in extensive application.
The one other possibility I can think of is that you might not have the cookie logic really well thought out visa vis what you are telling the cookies to be and when, how and when you are evaluating their values. Different browsers sometimes see the state of affairs that you are cuing these actions off of differently, if that is the problem, it must be taken into account in your logic.
jscheuer1
12-04-2007, 10:02 PM
Sounds like the logic then, but in your post you offer:
SetCookie("units", "Raw", 365);
Which is not the quirksmode code I recommend, perhaps it's just a typo. I cannot be sure (about either the code or the logic) without a link to the live page though.
Sounds like that might be unavailable. I can give you the basic outline of what I would do to begin to troubleshoot something like this. Say you have some code (using the quirksmode name for the function):
if(whatever)
createCookie("units", "Raw", 365);
The first thing you need to check is if the condition you are basing this upon is being seen by the browser. This is easily done:
if(whatever){
alert('attempting to set cookie units, value raw, days 365');
createCookie("units", "Raw", 365);
}
This strategy can be adapted to inline events like onclick, etc. If the browser doesn't alert, the problem isn't primarily the cookie code, it is that the condition or the event you are testing for hasn't been set/occurred at that point, as far as that browser sees it.
I can vouch for the quirksmode code working under IE 7. But, if there are issues surrounding your IE 7 settings, as might be indicated if it gives you the desired alert in a scenario like the above, but still doesn't set the cookie, this can be tested for by creating a very simple page with the sole purpose of setting and reading cookies, and loading that page in IE 7. You can also, as a preliminary test of the quirksmode code, try out the example (scroll down the page to Example) on their page I gave you the link to in IE 7. See if at least that works.
Oh, and I almost forgot, don't worry about finding the cookie in some directory or folder, even though (if you really know what you are doing) that can be a great diagnostic tool, but rather just see whether or not the cookie performs as expected.
Hi John,
I found out the problem. I was using the NETBIOS name to connect to my device. Apparently, IE7 will not create a cookie for a device named this way. When I switched to the IP address, it worked properly.
I'll have to search around tomorrow to find if IE7 can configure to work with NETBIOS names.
Again thanks for the help.
-Rob
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.