View Full Version : buidling a cookie

06-06-2007, 08:50 PM
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.

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?

06-07-2007, 12:17 AM
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.

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();
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;

06-07-2007, 03:42 PM
that's all greek to me,
how do I take that and make it only show my ad once per user.

06-07-2007, 06:37 PM
If you include this code, then use

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.