...

View Full Version : Cookie Problem



jaackson
12-18-2004, 08:17 AM
Hi,

I am using the following coding for accessing the cookie.
dokie = document.cookie
dokie=unescape(dokie);
start = dokie.indexOf("name=")+"name=".length;
end = dokie.indexOf(";", start);
if(end==-1)end = dokie.length
document.write(dokie.substring(start, end))

After entering the username, the username is stored in the cookie and it is accessed in another page as "Welcome Jackson"

But if i close the ie window again i go to that same url then i am getting as t=2 instead of jackson(username).
Please Help me.

Jack

Brandoe85
12-18-2004, 10:49 AM
Heres an example of what I kinda do when I set and Get cookies:


<html>
<head>
<title>Cookie</title>
<script language="javascript">
var expires = new Date();
expires.setFullYear(expires.getFullYear() + 1);
function set()
{
var name1 = document.forms[0].firstName.value;
document.cookie = encodeURI("fname="+name1) + "; expires=" + expires.toUTCString();
}
function getCookie()
{
var cookie = decodeURI(document.cookie);
var splitCookie = cookie.split("; ");
var name1 = splitCookie[0].split("=")[1];
document.getElementById('foo').innerHTML=(name1);
}
</script>
</head>
<body document.forms[0].firstName.focus();">
<form>
First Name <input type="text" name="firstName"><br>
<input type="button" value="cookie" onclick="set();">
<input type="button" value="Get Cook" onclick="getCookie();">
</form>
<div id="foo"></div>
</body>
</html>

jaackson
12-18-2004, 12:18 PM
hi,

Thanx for the sample coding. I can get the out put, but if i close the window and open a new window and goes to the same site instead of accessing the cookie name it displays as t=3. What could be the problem.

Thanx
Jack

Brandoe85
12-18-2004, 12:27 PM
The way you are retreiving your cookie is not persistant. When I run your code I don't get the right results each time. One time I will get the correct results, then the next i'll only get a few letters, and so on. How are you setting your cookie? If you try my example into your code im sure it will work.

Willy Duitt
12-18-2004, 04:04 PM
http://www.webdeveloper.com/forum/showthread.php?s=&threadid=51760
http://www.webxpertz.net/forums/showthread.php?t=31317



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum