The code you're currently using would perform as if I were logged in using facebook, if I had a cookie set containing the value 'fbsr_haha, gotcha'
Perhaps this is a job for regex:
/^|;\s*fbsr_[0-9]{4}\=[^;]+;/.test(document.cookie);
Should do the trick.
Expression explanation:
^|;: Either start of the string, or a semi-colon
\s*: followed by zero or more spacces
fbsr_[0-9]{4}: matches fsbr_ and any sequence of 4 numbers
\=: you could just write =, but sometimes you need to escape = (lookarounds), so best escape it once too often than not enough
[^;]+ anything but a semi-colon at least once or more
;: a literal semi-colon
Where to use this expression:
function checkCookie()
{
contentDiv=document.getElementById("cookieFb");
if (/^|;\s*fbsr_[0-9]{4}\=[^;]+;/.test(document.cookie))
{//facebooker!
contentDiv.style.display="none";
return;//return here, so you can leve the else out
}
contentDiv.style.display="block";
}
Well, that should do it.
However, I can't help myself, so if ever you get more familiar with JS, and feel like optimizing some of your scripts, consider this:
var checkCookie = (function(expr)
{
var contentDiv = document.getElementById('cookieFb');
return function()
{
contentDiv.style.display="block";
if (expr.test(document.cookie))
{
contentDiv.style.display="none";
}
};
}(/^|;\s*fbsr_[0-9]{4}\=[^;]+;/));
This is an optimized version of the exact same code you have. ATM, your code will query the dom for an element with the ID cookieFb every time it is called, and it'll create an instance of RegExp, too. By using a closure, you can query the DOM once, and create the instance of RegExp once, and re-use it for each call.
It's a bit more efficient, and micro-optimization in this case, but I couldn't help myself ;)
fbsrand the value2289. Any key value store is most useful if you know the keys in advance.document.cookie.split(/; */).map(function(c){return c.split('=')[0];})That should give you array of the cookie names only, without values.cookiename=foobar; expires=<some-date>; path=/;? you'd map the expiry dates and paths as cookies, too. and not all browsers support themapmethod yetmapis one of those functions you can expect everyone to have a SHIM for, but you have a good point aboutexpiresandpath.