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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    buidling a cookie

    I have made GreyBox popup automatically when a user visits the site, now I need to set a cookie so it only happens once whenever have a new announcement for our users.

    Code:
    {
    document.cookie = "splash_ck=splashpage;expires=Fri, 17 Aug 2007 20:47:11 UTC;path=/";
    if (splash_ck = splashpage)
    then (GB_showCenter('Watch this to see what we\'re capable of', 'splash.html', 284, 383);)
    else (do nothing)
    }
    i don't know javascript very well so I am trying to figure out how i should write the if statement.

    anyone able to help?

  • #2
    New Coder
    Join Date
    Apr 2007
    Location
    Silicon Valley California
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is standard cookie management code. You would use the 'readCookie' function and set it to a variable. Then test if that variable has a value or not. If it doesn't, then display your message and use 'createCookie' to set it.

    Code:
    var CookieMgr = {
    		 /**
    		 *	Creates a cookie with @ name set to value that expires in n days
    		 *	@param	name {String}						The name of the cookie
    		 *	@param	value {String}						The value of the cookie
    		 *	@param	days {String}						OPTIONAL: The number of days before cookie expires, otherwise expires at end of session
    		 */
    		createCookie: function(name, value, days) {
    			var expires=null;
    			if (days) {
    				var date = new Date();
    				date.setTime(date.getTime()+(days*24*60*60*1000));
    				expires = "; expires="+date.toGMTString();
    			}
    			else {expires = "";}
    			document.cookie = name+"="+value+expires+"; path=/";
    		},
    
    
    		/**
    		 *	Expires the cookie
    		 *	@param	name {String}						The name of the cookie
    		 */
    		eraseCookie: function(name) {
    			Core.getClient().createCookie(name, '', -1);
    		},
    
    
    		/**
    		 *	Retrieves the value of a cookie
    		 *	@param	name {String}						The name of the cookie
    		 *	@return	{String}							Value of the cookie or null
    		 */
    		readCookie: function(name) {
    			var nameEQ = name + "=";
    			var ca = document.cookie.split(';');
    			for(var i=0;i < 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;
    		}
    };

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that's all greek to me,
    how do I take that and make it only show my ad once per user.

  • #4
    New Coder
    Join Date
    Apr 2007
    Location
    Silicon Valley California
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you include this code, then use

    Code:
    if (CookieMgr.readCookie('cookieName')) {
    // execute code for when they have already visited your site
    }
    else {
    CookieMgr.createCookie('cookieName', true, lengthInDays);
    // execute code for first time visitors
    }
    where 'cookieName' is replaced with the string name of your cookie and 'lengthInDays' is replaced with a number such as 365 if you would like the cookie to not expire for a whole year.


  •  

    Posting Permissions

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