View Full Version : Help with read cookie function please

08-23-2002, 03:50 PM
Good morning!

I am trying to create a survey for our intranet. A survey pop up box pops up when the homepage is opened. When the survey is submitted, a cookie is written to the users computer. This way the cookie is only written if the user submits the survey. (I got this much to work. I'm so proud!)

Now the hard part. The next time the user opens the homepage, I want the script on the homepage to look and see if there is a cookie. If there is no cookie, the survey should pop up again. If there is a cookie, no pop up! This is the part I can't get to work. Do you know of any scripts out there that I can look to as an example? This is what I've tried. I've tried cookie=="" as well. Thank you so much for any thoughts on this! --Leisa

To set the cookie, with function called using onmousedown for the submit button:

//This sets a cookie

function setcookie(){
var mydate=new Date();
//advancing the year by 1 to expire

To read the cookie and open/not open the window:

function openwin(){
if (document.cookie!=""){
winref=window.open("ContactManagerSurvey.htm", "newwin",

<body bgcolor="#FFFFFF" text="#000000" topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0" onLoad="openwin();" link="#000066" vlink="#000066" alink="#000033">

08-23-2002, 06:40 PM

if (document.cookie) {

instead of:

if (document.cookie!="") {

It can also be used in the opposite manner:

if (!document.cookie) {
alert("no cookie found")

08-23-2002, 08:03 PM
Sorry, this doesn't work either. I think I'm in over my head with this one.

08-24-2002, 04:17 AM
This is the best cookie reader/setter/deleter that you can find. Essentially, you only have to worry about 3 of these scripts, as the rest are used by these 3: cookieSet(), cookieRead(), and cookieDelete(). You will probably also use cookieExpire() as well.

For cookieSet(), you have to set not only the name of the cookie (as you have), but also a value, and an expiration.

Before I get ahead of myself, here are the scripts. Personally, I just save all of this in a file called "cookies.js", and include it in my HTML page with <script language="javascript" src="cookies.js"></script>

aka "Night of the Living Cookie"
Updated: July 25, 1996
By Bill Dortch, hIdaho Design <bdortch@hidaho.com>
Released of the Public Domain
function FixCookieDate(date)
var base = new Date(0);
var skew = base.getTime(); // Dawn of (Unix) time. Should be zero.
if (skew > 0) date.setTime (date.getTime() - skew); // Mac OS - Ahead of it's time.

function cookieExpire(zNumberOfDays)
var exp = new Date();
exp.setTime(exp.getTime() + (zNumberOfDays*24*60*60*1000));

return exp;

function getCookieVal(offset)
var endstr = document.cookie.indexOf (";", offset);

if (endstr == -1) endstr = document.cookie.length;

return unescape(document.cookie.substring(offset, endstr));

function cookieRead(name)
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;

while (i < clen)
var j = i + alen;

if (document.cookie.substring(i, j) == arg) return getCookieVal (j);

i = document.cookie.indexOf(" ", i) + 1;

if (i == 0) break;

return null;

function cookieSet(name, value, expires, path, domain, secure)
document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");

function cookieDelete(name, path, domain)
if (cookieRead(name))
document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";

This is how I would go about doing what it seems like you want to do:

// Set the cookie on onSubmit this way.
// This will set a name, value (which you will have to check for later), and an expiration date of 365 days from now.

cookieSet('nameOfMyCookie', 'valueOfMyCookie', cookieExpire(365));

// Later on, when you need to read the cookie, you'd check it like this...

var myCookieValue=cookieRead('nameOfMyCookie');
if (myCookieValue != 'valueOfMyCookie') winref=window.open("ContactManagerSurvey.htm", "newwin", "width=380,height=450");

/ This will read the value of your cookie and send it into a variable, then checks the variable value against what it should/shouldn't be.

This is what I personally use, and it works GREAT!

I hope this helps!

08-26-2002, 02:56 PM
Well, I still can't get the initial pop up to happen. Can someone take a look at these files? Please email me at artsygal_00@yahoo .com if you are willing.