Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with read cookie function please

    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
    mydate.setYear(mydate.getYear()+1);
    document.cookie="name=mycookie;expires="+mydate.toGMTString();
    }

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

    function openwin(){
    if (document.cookie!=""){
    winref=window.open("ContactManagerSurvey.htm", "newwin",
    "width=380,height=450")
    }
    }


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

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try:

    if (document.cookie) {

    instead of:

    if (document.cookie!="") {



    It can also be used in the opposite manner:

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

  • #3
    New Coder
    Join Date
    Aug 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    nope...

    Sorry, this doesn't work either. I think I'm in over my head with this one.

  • #4
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

    Code:
    /***************************
    SETTING, READING, AND DELETING COOKIES
    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(); 
    	FixCookieDate(exp);
    	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:

    Code:
    // 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!

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #5
    New Coder
    Join Date
    Aug 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    still a no-go...

    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.

    ACK!
    Leisa


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •