JavaScript cookies work in IE but shouldn't when blocked

F

FishKeeper

I'm trying to write JavaScript code to test whether cookies are enabled
by writing a cookie and then reading its value. My code works fine in
Firefox, but it's not working in Internet Explorer 6. I have my
privacy settings set to block all cookies, but my JavaScript can read
and write cookies. Does this setting just block server cookies? That
doesn't seem right. I can't find any other settings that deal with
cookies in IE.

Here's a sample of my code which appears withinin the Body tag.

<script language="JavaScript">
//Verify that cookies are enabled for this site.
dtDate = new Date();
document.cookie = "MyTestCookie=MYWEB" + dtDate.toGMTString();

var strCookies = document.cookie;
var intPosition = strCookies.indexOf("MyTestCookie=MYWEB" +
dtDate.toGMTString());

alert(strCookies); //In IE, this displays my cookie that
SHOULDN'T get created.

if(intPosition==-1){
alert("Cookie not found!");
document.writeln("<tr><td height=\'30\'></td></tr>" +
"<tr><td align=\'center\'>" +
"<table border=\'1\' cellpadding=\'1\'
cellspacing=\'1\'><tr>"+
"<td align=\'center\'>IMPORTANT!!! Cookies
are not enabled. You must have cookies enabled to use this web
site.</td>" +
"</tr></table></td></tr>");
}
</script>
 
S

Sanjay

I am too lazy to try this myself, but I am not sure about IE though.

Sanjay
----------------------------------------------------------

//get the browser cookie support
var cookieEnabled=(navigator.cookieEnabled)? true : false;
//test manually if the flag is not present or false
if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled)
{
//alert("cookieEnabled flag is false or not defined doing another
check by creating the cookie");
if (getCookie("TestCookie") == null)
{
//alert("test cookie is not present hence will create it");
setCookie("TestCookie",0);
cookieEnabled = (getCookie("TestCookie") != null )? true : false;
deleteCookie("TestCookie");
}
else
{
cookieEnabled = true;
}
//alert("finally cookieEnabled flag is -->" + cookieEnabled);
}
----------------------------------------------------
function setCookie(name, value, expires, path, domain, secure)
{
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
------------------------------------------------------
function getCookie(name)
{
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0)
{
return null;
}
}
else
{
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1)
{
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
-------------------------------------------
function deleteCookie(name, path, domain)
{
if (getCookie(name))
{
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
---------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top