Cookie blocking issue with IE, javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WayneH
    New Member
    • Mar 2007
    • 1

    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?

    Thanks....

    Code:
    <html>
    <input type="button" value="test1" onclick="javascript:test1();">
    <input type="button" value="test2" onclick="javascript:test2();">
    <input type="button" value="kill all" onclick="javascript:killAll();">
    <div name="test" id="test"></div>
    <script language="JavaScript">
    function killAll(){
        killCookie('test1');
        killCookie('test2');
        document.getElementById("test").innerHTML='';
    }
    function killCookie(cookieName){
        if(document.cookie.indexOf(cookieName)!=-1){
            document.cookie=cookieName+"=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
        }
    }
    function test1(){
        var cookieName='test1';
        killCookie(cookieName);
        document.getElementById("test").innerHTML='';
        var today=new Date();
        var expire=new Date();
        expire.setTime(today.getTime()+60000);
        document.cookie=cookieName+"=;expires="+expire.toGMTString();
    
        if (document.cookie.indexOf(cookieName)!=-1){
            document.getElementById('test').innerHTML='<b>test1=Cookies are currently enabled in your browser.</b>';
        }else{
            document.getElementById('test').innerHTML='<b>test1=Cookies are NOT currently enabled in your browser.</b>';
        }
    }
    
    function test2(){
        var cookieName='test2';
        killCookie(cookieName);
        document.getElementById('test').innerHTML='';
        document.cookie=cookieName;
        if (document.cookie.indexOf(cookieName)!=-1){
            document.getElementById('test').innerHTML='<b>test2=Cookies are currently enabled in your browser.</b>';
        }else{
            document.getElementById('test').innerHTML='<b>test2=Cookies are NOT currently enabled in your browser.</b>';
        }
    }
    </script>
    </html>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    • Exsuscito
      New Member
      • Apr 2007
      • 1

      #3
      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.nam e" resolves to my PC anyway.

      Strange behaviour that accessing AJAX RPC methods (i.e. using ActiveX object 'Microsoft.XMLH TTP') 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.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Check out this link and see if it solves your problem.

        Comment

        Working...