BleepingComputer.com: Setting and getting cookies

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Setting and getting cookies

#1 User is offline   Ray Parrish 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 91
  • Joined: 30-October 08
  • Gender:Male
  • Location:Cottage Grove, Oregon

Posted 14 November 2008 - 08:11 AM

Ok, I'm having trouble setting a cookie. Here's my cookie code -

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 ;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return ""
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

function setStyle()
{
var Style=getCookie('Style');
if (Style!=null && Style!="")
  {
     document.getElementById("Skin").href= Style;
  }
}


And here is the call to set the cookie -

<div class="Blue" onmouseover="previewStyle('rayslinksblue.css');" 
onclick="setCookie('Style','rayslinksblue.css',365);">White on Blue</div>


This will not set the cookie. I am using the Web Developer toolbar in Firefox to
check for cookies after attempting to set it, and it reports there are no cookies set.

Can anyone see what is causing this to fail? I keep looking at it, and I can't
spot any mistakes...

Later, Ray Parrish

This post has been edited by Ray Parrish: 14 November 2008 - 08:18 AM


#2 User is offline   Ray Parrish 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 91
  • Joined: 30-October 08
  • Gender:Male
  • Location:Cottage Grove, Oregon

Posted 14 November 2008 - 09:45 AM

Hello again,

I found the problem... it wasn't that the cookie wasn't being set, it was that I was loading multiple onload statements in the body tag improperly and the setStyle() function wasn't being called on page load.

The cookie code is working perfectly now, and I'm on the way to having my Skin Selector written.

Later, Ray Parrish

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users