Cookie question

C

Creegan

Below I have a snippet of code for creating a cookie, but am having a
problem assigning data to it. I pass in values to the function which I
know are valid, then I assign this info to the cookie using
document.cookie. But when I try to view the cookie using the alert
function, the cookie is empty. Any ideas why this might be the case?

function setCookieValue
(cookieName,cookieValue,cookiePath,cookieExpires)
{
cookieValue = escape(cookieValue);
if (cookieExpires == "")
{
var nowDate = new Date();
nowDate.setMonth(nowDate.getMonth() + 6);
cookieExpires = nowDate.toGMTString();
}

if (cookiePath != "")
{
cookiePath = ";Path=" + cookiePath;
}

document.cookie = cookieName + "=" + cookieValue + ";expires=" +
cookieExpires + cookiePath;
alert(document.cookie);
}
 
E

Elus

U¿ytkownik Creegan napisa³:
Below I have a snippet of code for creating a cookie, but am having a
problem assigning data to it. I pass in values to the function which I
know are valid, then I assign this info to the cookie using
document.cookie. But when I try to view the cookie using the alert
function, the cookie is empty. Any ideas why this might be the case?

function setCookieValue
(cookieName,cookieValue,cookiePath,cookieExpires)
{
cookieValue = escape(cookieValue);
if (cookieExpires == "")
{
var nowDate = new Date();
nowDate.setMonth(nowDate.getMonth() + 6);
cookieExpires = nowDate.toGMTString();
}

if (cookiePath != "")
{
cookiePath = ";Path=" + cookiePath;
}

document.cookie = cookieName + "=" + cookieValue + ";expires=" +
cookieExpires + cookiePath;
alert(document.cookie);
}

There's many mistakes in your script. Why don't you use this one:
http://www.quirksmode.org/js/cookies.html#script

....or my own ;)
function sCookie(N,V,D){
document.cookie=N+'='+escape(V)+(D?'; expires='+new Date(+new
Date()+D*864e5).toGMTString():'')+'; path=/'
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top