problem getting info from a cookie with javascript
- by Jason
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;
}