Not able to detect cookies with javascript from this website
Status: New User - Welcome
Joined: 23 Sep 2008
Posts: 3
Reply Quote
Hi I have tried to detect the presence of cookies using the following link as posted by this website, however I have not been sucessful. It works in Firefox 3, Opera 9.51, 9.52 and Google Chrome. But NOT in IE7 Any help would be appreciated.
Back to top
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
MSIE 7 cookie detection works fine, I just double checked it.

I did discover one oddity with how MSIE checks / announces its cookie status: if you hit the site with cookies enabled, the cookie test page will correctly show cookies enabled, but if you then turn off cookies in the privacy part of MSIE 7 options, close msie, then revisit the cookie test page, it will still show cookies enabled.

Only by manually deleting all the cookies in the (god, doesn't Microsoft get sick of these horribly convoluted paths?):
c:\documents and settings\username\temporary internet files\
can you get MSIE to correctly state that cookies are in fact off. This is a fairly major bug in MSIE 7 I'd say, but I don't use the stuff except for testing so I don't really care.

I assume that MSIE is maybe assigning too long a life to session cookies, hard to say, whatever it is, it's a bug in MSIE from what I can see, but I can't say for sure, but it won't actually affect anyone but developers, since people will either have their cookies on or off on first page visit in the real world, so this is just of development interest.

Basically, the only part of your posting I'm responding to is the chance that the code itself had a bug. That is not the case, I'd post screenshots showing that MSIE 7 shows cookies enabled on the Javascript Cookie Test Page, but it just says: cookies: enabled, or, once I clear the temp internet files, restart msie 7 with cookies off, cookies: disabled, as it should.

We don't offer personal programming help here though, sorry, takes too much time.

However, if I remember right, that page code sets the cookie with php first, then tests it dynamically with javascript, so you probably have some issue with your php, I can't really tell you more than that though.
Back to top
Status: New User - Welcome
Joined: 23 Sep 2008
Posts: 3
Reply Quote
I have tried to delete all the temp files using the delete all in internet explorer and disk cleanup. I have even done what you suggested by going to documents and settings.
Back to top
Here is my code
Status: New User - Welcome
Joined: 23 Sep 2008
Posts: 3
Reply Quote
:: Code ::
<html xmlns="">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <title>Javascript Browser Sniffer (Example of use)</title>
    <style type="text/css">
    @import url("screen.css");

    <script type="text/javascript" language="javascript" src="brwsniff.js">

<script type="text/javascript" language="javascript">
                // remember, these are the possible parameters for Set_Cookie:
                // name, value, expires, path, domain, secure
                Set_Cookie( 'test', 'it works', '', '/', '', '' );
                if ( Get_Cookie( 'test' ) )
                    alert('Cookies Enabled');
                // and these are the parameters for Delete_Cookie:
                // name, path, domain
                // make sure you use the same parameters in Set and Delete Cookie.
                Delete_Cookie('test', '/', '');
                ( Get_Cookie( 'test' ) ) ? alert( Get_Cookie('test')) :
                alert( 'it is gone');
                     alert('Cookies Disabled');

    <%--<div id="main">
        <object id="pdfObj" classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="0"
        <asp:Panel ID="pMain" runat="server">
            <h3 style="padding-top: 0px; margin-top: 0px">
                Operating system and browser configurations</h3>
                You need javascript enabled in order to view this website properly. Please contact
                IT Help desk on 0508 4 IT HELP
            <asp:Label ID="lblDisplay" runat="server"></asp:Label>

            <%--<script type="text/javascript" language="javascript" src="example.js">

        <asp:Label ID="test" runat="server">

Here is the script
:: Code ::

  // To use, simple do: Get_Cookie('cookie_name');
    // replace cookie_name with the real cookie name, '' are required
    function Get_Cookie( check_name ) {
       // first we'll split this cookie up into name/value pairs
       // note: document.cookie only returns name=value, not the other components
       var a_all_cookies = document.cookie.split( ';' );
       var a_temp_cookie = '';
       var cookie_name = '';
       var cookie_value = '';
       var b_cookie_found = false; // set boolean t/f default f
       for ( i = 0; i < a_all_cookies.length; i++ )
          // now we'll split apart each name=value pair
          a_temp_cookie = a_all_cookies[i].split( '=' );
          // and trim left/right whitespace while we're at it
          cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
          // if the extracted name matches passed check_name
          if ( cookie_name == check_name )
             b_cookie_found = true;
             // we need to handle case where cookie has no value but exists (no = sign, that is):
             if ( a_temp_cookie.length > 1 )
                cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
             // note that in cases where cookie is initialized but no value, null is returned
             return cookie_value;
          a_temp_cookie = null;
          cookie_name = '';
       if ( !b_cookie_found )
          return null;

    only the first 2 parameters are required, the cookie name, the cookie
    value. Cookie time is in milliseconds, so the below expires will make the
    number you pass in the Set_Cookie function call the number of days the cookie
    lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

    Generally you don't need to worry about domain, path or secure for most applications
    so unless you need that, leave those parameters blank in the function call.
    function Set_Cookie( name, value, expires, path, domain, secure ) {

       // set time, it's in milliseconds
       var today = new Date();
       today.setTime( today.getTime() );
       // if the expires variable is set, make the correct expires time, the
       // current script below will set it for x number of days, to make it
       // for hours, delete * 24, for minutes, delete * 60 * 24
       if ( expires )
          expires = expires * 1000 * 60 * 60 * 24;
       //alert( 'today ' + today.toGMTString() );// this is for testing purpose only
       var expires_date = new Date( today.getTime() + (expires) );
       //alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

       document.cookie = name + "=" +escape( value ) +
          ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
          ( ( path ) ? ";path=" + path : "" ) +
          ( ( domain ) ? ";domain=" + domain : "" ) +
          ( ( secure ) ? ";secure" : "" );

    // this deletes the cookie when called
    function Delete_Cookie( name, path, domain ) {
       if ( Get_Cookie( name ) ) document.cookie = name + "=" +
             ( ( path ) ? ";path=" + path : "") +
             ( ( domain ) ? ";domain=" + domain : "" ) +
             ";expires=Thu, 01-Jan-1970 00:00:01 GMT";

Back to top
Status: Assistant
Joined: 04 Oct 2003
Posts: 594
Reply Quote
We don't use that code or method, as I noted, the cookie is set via php headers, then is tested and read by javascript after page load.

This works fine.

The only reason that initial my computer thing was written with all the tests in javascript was to avoid having to walk people through them breaking their pages by doing php headers incorrectly.

In general, that test works fine, but it sounds like you may have hit a bug or weakness, or possibly something built by design, in MSIE 7, that keeps it from registering a cookie immediately after it's been set, as you're currently doing now.

You could even play with putting a sleep timer between the first set and the get, experiment with it, see if it's a timer in msie 7, I'd start with say 1/10 second, and move up and see if you can find the point where it actually works, if any.

I won't speculate on why MSIE has this issue, you might try moving the initial cookie set to the very top of the page, and making the get call the very last thing you do before you write it.

But I can't say more than that, there's nothing wrong with the code, if MSIE can't handle it, or handles it wrong, or is making a decision you don't like in this case, that's out anyone's hand's but Microsoft.

However, again, note, setting this cookie in php, then reading it client side, works fine with MSIE 7.

Don't ask me to speculate on why this happens though, if you want to spend some hours working on the problem, feel free, if you try enough things, you might find out why the javascript method isn't working, but personally, this is why I stopped using most javascript stuff commercially, I got sick of something simple breaking in a new browser release.

If you want to learn why msie 7 does this, you can do lots of tests, one is to create a very long html page, set the cookie at the very top, and try to read it at the very bottom.

Just take any long blog comments thread or something or just repeat html over and over, then see how little html you can have between the set and the get for it to work. All very interesting if you find such things interesting, I don't anymore though, except to just generally know that there's some problem and how to resolve it if it comes up, but I almost never use javascript to set cookies anyway, I'd always do it server side, except for some tricks on done just to see how it works.
Back to top
Status: New User - Welcome
Joined: 18 Oct 2008
Posts: 1
Location: Ohio
Reply Quote
I may not be understanding this topic correctly, but I was having a devil of a time setting and reading cookies from javascript in IE7, IE6 and IE8beta, BUT only in one machine (a Windows XP home). On a Windows 95 running IE5.5 it worked fine, on a Windows 98 running IE6 it worked fine, and on a Windows XP Pro running IE6 it worked fine. Also Firefox 2 and 3 were working fine.

Now some background: I'm using the cookie plugin for jquery, which is all javascript. An ASPX page is reading the cookies, but the cookies are set in a normal HTML page. (not that any of that matters)

What was so annoying about the one machine where the cookies didn't work, was that the google analytics tracking code script always managed to set the cookes fine, just not my cookies were working. :P

After much playing around with the machine I finally realized two things were happening:

1. I was setting the href for the ASPX page using this format: (without the www) But I had bookmarked the website as (with the www) So whenever I used that machine, the HTML page I was on always had the "www" in its URL. But when the href was followed to the ASPX page there was NO "www" in the URL. So the ASPX could never read the cookie set by the "www" page. In fact I figured this out because the cookies files contained two cookies. One was owner@www.emblemagic.txt and the other was owner@emblemagic.txt.

So I proceeded to set a relative path in the javascript to the ASPX file instead of the URL which omitted the "www." That corrected the problem for the most part.

2. But there is still another problem. If one is setting cookies with the "www." URL but then starts using a non-www URL for the same site, for some reason, The cookies will either freeze in one setting (depending on their expiration) and thereby would not seemingly be updated. So the solution to this is, if you start using THE SAME URL differently, such as one WITH www and then start using it some time later without the www then you will have to completely delete all the cookies, to get them to synchronize again.

These findings relate only to cookies set with javascript, on IE. Although Firefox did better, it also showed problems if you switched the way you reached your site's URL (with or without the WWW) Of course, in the real world, normal people don't do this, I wouldn't think. But developers might. If one goes to my site from a Google search, the www URL is followed. But in my own direct addressing, I used the non-www code. It was definitely an error on my part to use a full URL in the javascript to reach the ASPX page, and now using the relative path should have the cookies working flawlessly on any computer however the URL is reached.

In conclusion, I don't think it's anything to worry about, but it's good to know anyway.
Back to top
Display posts from previous:   

All times are GMT - 8 Hours