472,119 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Cookie blocking issue with IE, javascript

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
3 2455
16,027 Expert Mod 8TB
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
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 http://domain.name/xxx/yy, where "domain.name" 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
16,027 Expert Mod 8TB
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.

Similar topics

6 posts views Thread by Mark | last post: by
15 posts views Thread by Oleg Leikin | last post: by
2 posts views Thread by RootShell | last post: by
5 posts views Thread by cbhoem | last post: by
16 posts views Thread by Stevo | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.