Click to See Complete Forum and Search --> : javascript cookies


karkboyd
03-11-2003, 03:37 PM
I am new at this, taking a class. I am supposed to write some cookies using javascript to create 1) a "welcome, return user" on my main page, and 2) a "you have visited this page x times" on my thank you page at the end. the site is a fake boot shop. My problem is that when I wrote the script for the second part, it messed up the cookie on my first page, which now seems to be reading something from the script I wrote for the second page, and I cannot figure out why. Like i said, I am very new at this! If someone who knows about this oculd view my page at http://userpages.umbc.edu/~karissa1/morganwebsitejava/frameset.html
and tell me how to fix this? HELP!:confused:

dabush
03-12-2003, 11:20 PM
ok. right now, you pages are not even reading the cookies. anyways, be more specific.

on which pages (1, 2 or both) would you like the browser to display the name and they number of times they have been at the site. then i will write u a script!;)

karkboyd
03-13-2003, 06:07 AM
On the first page, I want it to have a hello box where the user can enter a name, which then each time they return it says,"Welcome, yourname". Then, at the very end, in the thank you page, after the client has 'purchased' a boot, I want it to read "you have purchased boots from this site X times." I need to do this using cookies and javascript.
This is a fake website- it is for learning purposes only. To get through the site to the end you can just enter any old fake numbers and e mail address. Then you can see all the pages.
My problem has been getting the right wording to appear on the right pages. I know that the cookies are read by the users computer- and when I tried to write the "you have purchased boots..." for the last page, it started to interfere with the first page's wording. If you could write me a script that works then I can see how it all goes together! I need to see how to make the pages read the cookies, too. Thank you very much!

dabush
03-13-2003, 09:28 AM
i understand. i'll be making that script right away!

dabush
03-13-2003, 10:15 AM
This is for your homepage...the other one is on its way and will not affect this one!

STEP 1
Add the following between <HEAD> and </HEAD>

<script language=JavaScript type=text/javascript>
<!--
function readCookie()
{
if ((document.cookie) && (document.cookie != ''))
{
var big_cookie = document.cookie;
var beg = big_cookie.indexOf('bootshop_name')
if (beg != -1)
{
beg += 14;
end = big_cookie.indexOf(';',beg);
if (end == -1) end = big_cookie.length;
return 'Hello, '+unescape(big_cookie.substring(beg,end));
}
else
{
return 'Enter your name:<input type=text size=30 onBlur="setCookie(this.value);">';
}

}
else
{
return 'Enter your name:<input type=text size=30 onBlur="setCookie(this.value);">';
}
}
function setCookie(name)
{
if (name != '')
{
var date = new Date("December 31, 3000");
var exp_date = date.toGMTString();
var the_cookie = escape(name);
document.cookie = 'bootshop_name='+the_cookie+';expires='+exp_date;
welcomeMsg.innerHTML = 'Hello, '+name;
}
}
// -->
</script>



STEP 2
Add the following where you want the name displayed between the <BODY> and </BODY> tags. Delete the stuff you already have there.

<script language=JavaScript type=text/javascript>
<!--
document.write(readCookie());
// -->
</script>