Skip to content

Commit

Permalink
totally redid cookies test. thx @tauren. fixes 191
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Dec 12, 2011
1 parent e8cc0f6 commit 33f00fb
Showing 1 changed file with 13 additions and 31 deletions.
44 changes: 13 additions & 31 deletions feature-detects/cookies.js
@@ -1,33 +1,15 @@

// cookies?
// both delicious and useful.
// by tauren
// https://github.com/Modernizr/Modernizr/issues/191

Modernizr.addTest('cookies', function () {
// Quick test if browser has cookieEnabled host property
if (navigator.cookieEnabled) return true;
// Create cookie
document.cookie = "cookietest=1";
var ret = document.cookie.indexOf("cookietest=") != -1;
// Delete cookie
document.cookie = "cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";
return ret;
});

(function(){

// FIXME: we could do this with less code.
function createCookie(name,value) {
document.cookie = name+"="+value+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}


var random = '' + Math.round(Math.random() * 1e8);
createCookie('Modernizr', random);
Modernizr.cookies = readCookie('Modernizr') == random;
eraseCookie('Modernizr');

})();

0 comments on commit 33f00fb

Please sign in to comment.