• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. Browser Feature Detection > 5.9. Detecting Cookie Availability

Detecting Cookie Availability

NN 2, IE 3

Problem

You want your scripts to know whether the user’s browser has cookies enabled.

Solution

Internet Explorer 4 or later and Netscape 6 or later feature the navigator.cookieEnabled property, which you can test at any place within a script:

if (navigator.cookieEnabled) {
    // invoke cookie statements here
}

For earlier versions of these browsers, you can test whether cookies are enabled by first checking for the presence of a value stored in the cookie. If no data is there, you can test write a cookie to see if it “sticks”:

var cookieEnabled = false;
if (typeof document.cookie =  = "string") {
    if (document.cookie.length =  = 0) {
        document.cookie = "test";
        cookieEnabled = (document.cookie =  = "test");
        document.cookie = "";
    } else {
        cookieEnabled = true;
    }
}

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint