1

How would I detect if the website has set a cookie in general without checking lots of individual cookies.

I looked at this similar question: Create a cookie if (and only if) it doesn't already exist. But the solutions provided there check if a specific cookie exist, not just cookies in general.

I have tried:

if($.cookie) {  
 //code   
} 
Community
  • 1
  • 1
RSM
  • 14,540
  • 34
  • 97
  • 144

3 Answers3

2

jQuery really isn't necessary to check if the current domain has set a cookie in the user's browser, just use raw JS:

if(document.cookie.length > 0) {
    // do stuff ...
}
Alex
  • 34,899
  • 5
  • 77
  • 90
2

check if cookie exists:

if (document.cookie === "") {
  // do something if no cookie exists
}

I would not use jquery for this to safe memory/processing

Edditoria
  • 199
  • 1
  • 15
  • document.cookie will always be a string, and `"" != null` – Niko Jun 08 '12 at 12:06
  • wouldn't `!= null` interpret as `if document.cookie is not equal to null` which therefore means if cookie exist. so shouldnt it be `==` – RSM Jun 08 '12 at 12:06
1

You don't need to use the $.cookie plugin for this:

if (document.cookie) {
    // at least one cookie has been set...
}
Niko
  • 26,516
  • 9
  • 93
  • 110