/    Sign up×
Community /Pin to ProfileBookmark

Trying to see if cookies enabled

The following code isn’t working. What I want to do is to write to the page or alert the user that they don’t have cookies enabled if this in fact the case from their browser settings. This is to allow the user to check a checkbox so they don’t see an informational screen again when they come back to our application. Any suggestions are welcome! Thanks!

So, here’s what’s in my <HEAD> tag:

<script language=”javascript”>

document.cookie = “cookiesenabled4=yes”;

function createCookie(name,value,days)

{

if (document.cookie.indexOf(“cookiesenabled4=”) == -1)//means the user has cookies disabled on their browser

{

document.write(“Please enable cookies in order to allow this window to be avoided, if desired, in the future!”);

alert(“You must allow cookies!”);

}

else//cookies are allowed, so set this one from the checkbox1 onclick event

{

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=/”;

}

</script>


**************************************

Here’s what I have for my checkbox onclick event:


**************************************

<input id=”Checkbox1″ type=”checkbox” onclick=”createCookie(‘ppkcookie3′,’testcookie’,60);”/>

Don’t show me again!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@_Aerospace_Eng_Dec 29.2006 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Checking to see if cookies are enabled&lt;/title&gt;
&lt;style type="text/css"&gt;
#nocookies {
display:none;
}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
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 &lt; 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);
}
window.onload = function()
{
createCookie('isEnabled','1',9999); // attempt to create test cookie
if(readCookie('isEnabled') == null) // get cookie value, if null, then it means cookie was not created
{
var text = document.createTextNode('You have cookies disabled, therefore the site may not work as intended.');
document.getElementById('nocookies').appendChild(text);
document.getElementById('nocookies').style['display'] = 'block';
alert('You must allow cookies!');
}
else if(readCookie('isEnabled') == '1') // if cookie value is 1 then set the onclick of the input box
{
document.getElementById('Checkbox1').onclick = function()
{
createCookie('ppkcookie3','testcookie',60);
}
eraseCookie('isEnabled'); // erase previous test cookie
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="nocookies"&gt;&lt;/div&gt;
&lt;form&gt;
&lt;input id="Checkbox1" type="checkbox" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@Khalid_AliDec 29.2006 — you can use the following code to see if the cookies are enabled?

var isCookieEnabled = (navigator.cookieEnabled)?true:false;

alert("cookies are enabled?= "+isCookieEnabled);
Copy linkTweet thisAlerts:
@_Aerospace_Eng_Dec 29.2006 — Hmm totally forgot about that method. Using that method the new code would be
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Checking to see if cookies are enabled&lt;/title&gt;
&lt;style type="text/css"&gt;
#nocookies {
display:none;
}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
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 &lt; 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);
}
window.onload = function()
{
if(!navigator.cookieEnabled) // Use browser feature to check for cookies
{
var text = document.createTextNode('You have cookies disabled, therefore the site may not work as intended.');
document.getElementById('nocookies').appendChild(text);
document.getElementById('nocookies').style['display'] = 'block';
alert('You must allow cookies!');
}
else // if navigator.cookieEnabled returns true then do the following
{
document.getElementById('Checkbox1').onclick = function()
{
createCookie('ppkcookie3','testcookie',60);
}
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="nocookies"&gt;&lt;/div&gt;
&lt;form&gt;
&lt;input id="Checkbox1" type="checkbox" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@bubberzauthorDec 29.2006 — Trying this, but no luck. Even with cookies disabled, it tells me they are enabled:

<head>

<title>Untitled Page</title>

<script language="javascript">

function testcookie()

{

if (navigator.cookieEnabled == 0)

{

alert("You need to enable cookies for this site to load properly!");

}

else

{

alert("good...cookies are enabled");

alert("the cookie was set");

var name = "ppkcookie5";

var value = "testcookie";

var days = 60;

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=/";
}
}


</script>

</head>

<body>

<input id="Checkbox1" type="checkbox" onclick='testcookie()'/>

Don't show me again!
Copy linkTweet thisAlerts:
@_Aerospace_Eng_Dec 29.2006 — I just tried what you posted and it works fine. It seems that you really aren't disabling cookies when you think you are. If you truly disable cookies then the moment you reload the forum it will say you aren't logged in. What browser are you using? I'm guessing IE.
Copy linkTweet thisAlerts:
@bubberzauthorDec 29.2006 — What I do is Tools/Options/Privacy and go all the way to the top to disable cookies. I'm using IE 6 sp2.

When I do that, and refresh this forum page...it tells me that I have cookies disabled.

Then, when I re-run the code, I still get the two alerts:

alert("good...cookies are enabled");

alert("the cookie was set");

...from this code:

<head>

<title>Untitled Page</title>

<script language="javascript">

function testcookie()

{

if (navigator.cookieEnabled == 0)

{

alert("You need to enable cookies for this site to load properly!");

}

else

{

alert("good...cookies are enabled");

alert("the cookie was set");

var name = "ppkcookie5";

var value = "testcookie";

var days = 60;

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=/";
}
}


</script>

</head>

<body>

<input id="Checkbox1" type="checkbox" onclick='testcookie()'/>

Don't show me again!

</body>

</html>
Copy linkTweet thisAlerts:
@konithomimoDec 29.2006 — If you have it set to "Block All Cookies" then it should work as intended.
×

Success!

Help @bubberz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...