473,414 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

JavaScript cookies work in IE but shouldn't when blocked

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>

Feb 17 '06 #1
1 2460
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";
}
}
---------------------------------------------

Feb 17 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: duane | last post by:
Dear Experts: I have a problem with my JS cookies. I am setting cookies in files located in two different directories, e.g. one in ROOT, one in ROOT/dir1/dir2. The cookies are then sent to PHP...
1
by: John Taylor-Johnston | last post by:
I'm a University academic looking for a proper definition of JavaScript Cookies. http://www.CollegeSherbrooke.qc.ca/languesmodernes/604-HAE_Grammar_Practice/ I'm trying to decipher what...
7
by: Bert | last post by:
I have been reading the post and the FAQ and have been unable to find anything that will help with my problem. First let me say that I am not a web developer, designer and no next to nothing...
3
by: Magnus | last post by:
I have a iframe inside a main html file. The iframe is used to display various html files. Inside the html files displayed in the iframe I want to have javascript to set certain global cookies,...
1
by: David M. Gauntt | last post by:
I have been testing some javascript for my professional society web page, and found that I couldn't get Safari to save any cookies. No matter what I assign to document.cookie, I get an empty...
1
by: Rabel | last post by:
Hi I am very new to creating cookies and when I say new I mean I never had created one, but now I have decided to use one, I have a flash website that I want to take new users to a different page,...
5
by: thatsMaBoy | last post by:
Hi, I am attempting to display a simple Javascript alert when a webpage loads. It works on some pages but not on others. For example, the following code does not produce an alert: Code in...
7
by: mark4asp | last post by:
How can I prevent Caching of JavaScript and CSS files ONLY when I deploy a new application? I only want to force a refresh the first time the client uses the new build. For instance, I'm told I...
2
by: cleary1981 | last post by:
Hi, I have set two cookies in my previous page. When I use an alert to show the value of the cookies this is the format I get. auth=1;project=4 I wan to use javascript to get the value of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.