Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to see if cookies enabled

Status
Not open for further replies.

adddeveloper

Programmer
Dec 16, 2006
43
US
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!
 
I wrote an example using my generic cookie functions. I copied them from the web about 3 years ago, and have since forgotten where they came from. So.... I can't give proper credit where credit is due:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

//do a cookie check
document.cookie = "testCookie";
var cookiesEnabled = (/testCookie/.test(document.cookie)) ? true : false;
//clear the dummy value
document.cookie = "";

//check to see if the message is stifled
var showMessage = true;
if (cookiesEnabled) {
   showMessage = (getCookie("ppkcookie3") == null) ? true : false;
}


window.onload = function () {
   if (showMessage) {
      alert("You'll keep seeing this till ya set that cookie!");
   }
};

//generic cookie functions
function getCookie(name) {
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
}

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 createCookie(name, value, days) {
   var dte = new Date();
   dte.setTime(dte.getTime()+(days*24*60*60*1000));
   if (cookiesEnabled) {
      setCookie(name, value, dte);
   }
   else {
      alert("enable your cookies or you'll keep seeing that message!");
   }
}

</script>
<style type="text/css"></style>
</head>
<body>

<input id="Checkbox1" type="checkbox" onclick="createCookie('ppkcookie3','testcookie',60);"/>

</body>
</html>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
kaht,

I'm trying this, but even with Cookies disabled, I still get the alerts that they are enabled:

<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>
<input id="Checkbox1" type="checkbox" onclick='testcookie()'/>
Don't show me again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top