problem getting info from a cookie with javascript
Posted
by Jason
on Stack Overflow
See other posts from Stack Overflow
or by Jason
Published on 2010-03-29T00:37:34Z
Indexed on
2010/03/29
0:43 UTC
Read the original article
Hit count: 441
JavaScript
|cookies
I am having an issue with my cookies and I can't figure it out. Basically I have it set up so it checks for the cookie to see if the user is logged in, and then displays either a welcome message or a login link. It works - except that instead of returning the persons name in the welcome message it just is blank where the name should be. The cookie is there, with all the appropriate info.. not sure what I am doing wrong.
var itm = new Array();
itm[0] = findCookie("ui");
if (itm[0] == null) {
document.write("<h2><a href='logreg.html'>Log In or Sign Up</a></h2>");
}
else {
var c1 = itm[0].indexOf(",");
var c2 = itm[0].indexOf(",",c1);
var c3 = itm[0].indexOf(",",c2);
var gname = itm[0].substring(c2,c3);
document.write("<h2>Welcome "+gname+"!</h2>");
}
The findCookie function is..
function findCookie(val){
var cookie = null;
var findVal = val + "=";
var dc = document.cookie;
if (dc.length > 0)
{
var start = dc.indexOf(findVal);
if (start >= 0)
{
start += findVal.length;
lastVal = dc.indexOf(";", start);
if (lastVal == -1)
{
lastVal = dc.length;
}
cookie = (dc.substring(start, lastVal));
}
else
{
return cookie;
}
}
return cookie;
}
© Stack Overflow or respective owner