By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,550 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,550 IT Pros & Developers. It's quick & easy.

Cookie blocking issue with IE, javascript

P: 1
Hi -

I'm trying to use javascript to determine if a user's browser has cookies enabled or not.

To test: copy this code into a file with a 'html' extension, and load it into your IE browser directly, through a local web server and through a remote web server.
By turning cookies on/off in your browser, running the file, then clicking the 'test1' or 'test2' buttons, the displayed text should accurately inform you if your cookies are On or Off.

FireFox behaves as expected, but IE 6 does not (surprised?).

When this script below is run from an external server, it works on IE.
But if I simply load the page 'locally' in my IE browser (drag-n-drop or File>Open), it sees that cookies are turned off. As well, if I load it via IIS on an internal network, it never sees that cookies are turned off.

It's as though it must only come from an external server to work - why?
How can I make it work in IE 6 regardless of where the page came from?


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <input type="button" value="test1" onclick="javascript:test1();">
  3. <input type="button" value="test2" onclick="javascript:test2();">
  4. <input type="button" value="kill all" onclick="javascript:killAll();">
  5. <div name="test" id="test"></div>
  6. <script language="JavaScript">
  7. function killAll(){
  8.     killCookie('test1');
  9.     killCookie('test2');
  10.     document.getElementById("test").innerHTML='';
  11. }
  12. function killCookie(cookieName){
  13.     if(document.cookie.indexOf(cookieName)!=-1){
  14.         document.cookie=cookieName+"=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
  15.     }
  16. }
  17. function test1(){
  18.     var cookieName='test1';
  19.     killCookie(cookieName);
  20.     document.getElementById("test").innerHTML='';
  21.     var today=new Date();
  22.     var expire=new Date();
  23.     expire.setTime(today.getTime()+60000);
  24.     document.cookie=cookieName+"=;expires="+expire.toGMTString();
  26.     if (document.cookie.indexOf(cookieName)!=-1){
  27.         document.getElementById('test').innerHTML='<b>test1=Cookies are currently enabled in your browser.</b>';
  28.     }else{
  29.         document.getElementById('test').innerHTML='<b>test1=Cookies are NOT currently enabled in your browser.</b>';
  30.     }
  31. }
  33. function test2(){
  34.     var cookieName='test2';
  35.     killCookie(cookieName);
  36.     document.getElementById('test').innerHTML='';
  37.     document.cookie=cookieName;
  38.     if (document.cookie.indexOf(cookieName)!=-1){
  39.         document.getElementById('test').innerHTML='<b>test2=Cookies are currently enabled in your browser.</b>';
  40.     }else{
  41.         document.getElementById('test').innerHTML='<b>test2=Cookies are NOT currently enabled in your browser.</b>';
  42.     }
  43. }
  44. </script>
  45. </html>
Mar 9 '07 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 15k+
P: 16,027
Welcome to The Scripts (TSDN)..

This may not be the problem, but IE can be picky about forms. You should define form tags. Also you have no body tags.
Mar 9 '07 #2

P: 1
I'm having the same issue (I believe) with an AJAX RPC call. For some reason only RPC server methods receive an old / stale cookie while all other standard pages work correctly and always receive the up-to-date cookie.

This doesn't happen in FireFox, but it does in IE 6, and it only occurs when I am calling http://localhost/xxx/yy. I am able to work-around it if I access, where "" resolves to my PC anyway.

Strange behaviour that accessing AJAX RPC methods (i.e. using ActiveX object 'Microsoft.XMLHTTP') via http://localhost/xxx/yy would cause IE to send a stale cookie.

However this is a hassle, and not an ideal solution for longterm application development on a small LAN.
Apr 10 '07 #3

Expert Mod 15k+
P: 16,027
Check out this link and see if it solves your problem.
Apr 11 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.