...

View Full Version : Checking if cookies enabled



tdavis
10-18-2006, 10:35 PM
I have seen quite a bit posted about this, but nothing related to the different levels of privacy in Microsoft Explorer 6.0 under the internet options. I am using the script below to determine if cookies are on, but it looks like it only works if the settings are "Accept all cookies" or "Low". The default is "Medium" and the script fails at that setting or above. Do I have to require that the setting be higher than medium to use this code?

document.cookie = "TemporaryTestCookie=yes;";
if(document.cookie.indexOf("TemporaryTestCookie=") == -1) {
alert("Cookies are required to use this web site!\nPlease click the link at the bottom of the page\nand review how to enable cookies! ");
}
else {; }

Thanks,
-tdavis

_Aerospace_Eng_
10-19-2006, 01:08 AM
Try something like this. On page load set the cookie then after setting it read the cookie. If null is returned it means the cookie wasn't set therefore the alert should come up.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function createCookie(name,value,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; 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;
}
window.onload = function()
{
createCookie('testcookie','true',9999);
if(readCookie('testcookie') == null)
{
alert('Cookies are required to use this web site!\nPlease click the link at the bottom of the page\nand review how to enable cookies!');
}
}
</script>
</head>

<body>
</body>
</html>

tdavis
10-19-2006, 01:51 AM
That works a lot better! Thanks!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum