Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-18-2004, 08:17 AM   PM User | #1
jaackson
New to the CF scene

 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jaackson is an unknown quantity at this point
Unhappy Cookie Problem

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
jaackson is offline   Reply With Quote
Old 12-18-2004, 10:49 AM   PM User | #2
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Heres an example of what I kinda do when I set and Get cookies:
Code:
<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>
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 12-18-2004, 12:18 PM   PM User | #3
jaackson
New to the CF scene

 
Join Date: Dec 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jaackson is an unknown quantity at this point
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
jaackson is offline   Reply With Quote
Old 12-18-2004, 12:27 PM   PM User | #4
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
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.
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 12-18-2004, 04:04 PM   PM User | #5
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
http://www.webdeveloper.com/forum/sh...threadid=51760
http://www.webxpertz.net/forums/showthread.php?t=31317

Last edited by Willy Duitt; 12-18-2004 at 04:07 PM..
Willy Duitt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:43 PM.


Advertisement
Log in to turn off these ads.