Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie Java Script Not Working. Help please.

Status
Not open for further replies.

Shadiman

Programmer
Joined
Jan 30, 2006
Messages
5
Location
US
Dear All,

I am new to Java and Java Script.

I have a need to read a cookie and update the expiry date on a particular cookie; so I have written the following;

function Update_Cookie(){
// Write a set of cookies

document.cookie = "Session_Tolken=123"
document.cookie = "First_name=Shad"
document.cookie = "Second_name=Mortazavi"

// Script
var expireDate = new Date
expireDate.setSeconds(expireDate.getSeconds()+60)

var cookieName
var cookieValue
IfaceCookie = document.cookie.split(";")
document.write (IfaceCookie + "<BR>")
for (var i = 0; i < IfaceCookie.length; i++)
{
var cookieName = IfaceCookie.split("=")[0];
var cookieValue = IfaceCookie.split("=")[1];

document.write ("i = "+i+" CookieName ="+cookieName +"<BR>")

if (cookieName == 'Session_Tolken')
{
alert ('in')
document.cookie = "Session_Tolken="+cookieValue+";expires="+expireDate;
}
}

}

This works if the cookies are writtent in the order specified above, it breaks if I change the order in which the cookie is written?

document.cookie = "First_name=Shad"
document.cookie = "Session_Tolken=123"
document.cookie = "Second_name=Mortazavi"

The stange think is that the write statment in the script indicates that eveything is OK;

First_name=Shad, Session_Tolken=123, Second_name=Mortazavi
i = 0 CookieName =First_name
i = 1 CookieName = Session_Tolken
i = 2 CookieName = Second_name

I'm sure I am missing something obvious.
 
The problem was caused by a leading space in the variable depending on where in the cookie the value pair was.

I have re-writtent the script;

function Update_Cookie(){
// Write a set of cookies

document.cookie = "Session_ID=15"
document.cookie = "First_name=Shad"
document.cookie = "Session_Tolken=123"
document.cookie = "Second_name=Mortazavi"

var expireDate = new Date
expireDate.setSeconds(expireDate.getSeconds()+60)

var cookieName
var cookieValue

IfaceCookie = document.cookie.split(";")
document.write (IfaceCookie + "<BR>")
for (var i = 0; i < IfaceCookie.length; i++)
{
var cookieName = IfaceCookie.split("=")[0];
var cookieValue = IfaceCookie.split("=")[1];

// Work out if there is a session tolken
// Cookies may have a space at the start

if (cookieName == ' Session_Tolken' || cookieName =='Session_Tolken')
{
document.cookie = "Session_Tolken="+cookieValue+";expires="+expireDate;
} else if (cookieName == ' Session_ID' || cookieName =='Session_ID')
{
document.cookie = "Session_ID="+cookieValue+";expires="+expireDate;
}
}


}

</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top