Click to See Complete Forum and Search --> : I need some help...


Eag|e
03-03-2003, 06:33 PM
If anyone read my post earlier today, you would know that I have only been doing JS for about 2 days...well on Art Of Camoflauge (http://www.ghillie.vze.com) I have it made using HTML, but I want to add something more to it. So as you see when you load the site, it brings up a prompt asking for your name, but everytime you go to the site it asks for your name! I was wondering if there is some way to make it so that it makes a cookie or something so that they don't have to enter it each time, but if they are on another computer they have to. That was the main thing about the post, but there is something else I want to do with the prompt thing asking for your name. I also want to make it so that their name can be used on other pages without them entering it again. Thanks.

-Eag|e

Jona
03-03-2003, 07:44 PM
You can get it on any page if you have the explicity named cookie. Just set the prompt's value to a cookie. Do you need code for the cookie?

Eag|e
03-03-2003, 07:46 PM
Yeah dude, I need to know like how to do it all, Lol. I'm like totally new to JavaScript and stuff...

Jona
03-03-2003, 07:54 PM
OK, we have two pages: page1.html and page2.html (for example). The source code of page1.html is...


<html><head><title>Fun with Cookies</title>
<script>
cookie_name = "yourName";
var YourName;
function putCookie() {
if(document.cookie != document.cookie) {index = document.cookie.indexOf(cookie_name);}
else { index = -1;}
if (index == -1){ YourName=prompt("What\'s your name?","");
document.cookie=cookie_name+"="+YourName+"; expires=Monday, 04-Apr-2004 05:00:00 GMT";
} }
</script>
</head><body onload="putCookie()">
<h1><a href="page2.html">Click here now>>></a></h1>
</body></html>


And on page2.html...


<html><head><title>Fun with cookies: Your name is...</title></head>
<body>
<script>
cookie_name = "yourName";
var YouWrote;
function getName() {
if(document.cookie){ index = document.cookie.indexOf(cookie_name);
if (index != -1) {
namestart = (document.cookie.indexOf("=", index) + 1);
nameend = document.cookie.indexOf(";", index);
if (nameend == -1) {nameend = document.cookie.length;}
YouWrote = document.cookie.substring(namestart, nameend);
YouWrote = YouWrote.split("-").join("");
return YouWrote; } } }
YouWrote=getName();
document.write("<h1>Your name is.... "+YouWrote+"!!!</h1>");
document.title+=YouWrote; }
</script>
</body></html>


Try that on for size.... :D

Eag|e
03-03-2003, 08:37 PM
THANKS DUDE! I'll try it right when I get back on the computer! But my brother is getting on right now, so I'll let you know if it works alot! THANKS ALOT!

Eag|e
03-04-2003, 06:42 PM
Ok, I'm sorry to keep bothering you Jona, but I kinda don't understand this! The way the site is set up is it has 3 frames, it has the top frame that holds the Flash banner, it is called Banner.html Then I got the nav bar, which is a seperate frame called LeftCol.html then I got the body which is called RightCol.html Can you please explain what exactly I have to do, and to let you know right now I have the prompt loading on RightCol.html Thanks. - Eag|e

Jona
03-04-2003, 06:51 PM
The script should work fine across frames. It's using a cookie. Make a link to another page that has the source code of "page2.html" in it.

Jona
03-04-2003, 06:59 PM
Sorry, I messed up on the code. I hate it when I put a bracket in the wrong place! Here is page2.html again...


<html><head><title>Fun with cookies: Your name is...</title></head>
<body>
<script>
cookie_name = "yourName";
var YouWrote;
function getName() {
if(document.cookie){ index = document.cookie.indexOf(cookie_name);
if (index != -1) {
namestart = (document.cookie.indexOf("=", index) + 1);
nameend = document.cookie.indexOf(";", index);
if (nameend == -1) {nameend = document.cookie.length;}
YouWrote = document.cookie.substring(namestart, nameend);
return YouWrote; } } }
YouWrote=getName();
document.write("<h1>Your name is.... "+YouWrote+"!!!</h1>");
document.title+=YouWrote
</script>
</body></html>

Eag|e
03-04-2003, 09:35 PM
Lol, well thanks for lookin at the code again, but you didn't quite answer the question I had needed answered :/ I need to know like if I have Page1.html load first, then make Page2.html basically the home page like RightCol.html is. Please get back to me brotha, thanks. - Eag|e

Jona
03-05-2003, 12:56 PM
OK, page2.html's script will display the name of the user. Every time you want to use the person's name, you use this snippet of code: <script>YouWrote=getName();
document.write("<h1>Your name is.... "+YouWrote+"!!!</h1>");</script>

That's basically it, it will work on any page you put it on after they are prompted once. And, if they've already been prompted, they shouldn't receive the prompt again (if they do, please tell me).

Eag|e
03-05-2003, 06:11 PM
www.gamingshack.net/ghillie/Page1.html (http://www.gamingshack.net/ghillie/Page1.html) Ok, there is the link to the site and it works! It wasn't working at school though, but I think it is because the web browser is set to not allow cookies :/ O well, I can just change it later. Lol. Thanks alot though dude! YOU RULE!

Eag|e
03-05-2003, 06:12 PM
Lol, just clicked the link after I posted that, and I had already entered my name, but it still brought up the prompt for Page1.html

Jona
03-05-2003, 08:30 PM
It shouldn't prompt you for your name again. If it does, then something is wrong, try doing this and see if it works...


<html><head><title>Fun with Cookies</title>
<script>
cookie_name = "yourName";
var YourName;
function putCookie() {
if(document.cookie != document.cookie) {index = document.cookie.indexOf(cookie_name);}
else { index = -1;}
if (index == -1){
if(document.cookie.value.indexOf(cookie_name)){ } else { YourName=prompt("What's your name?","");
document.cookie=cookie_name+"="+YourName+"; expires=Monday, 04-Apr-2004 05:00:00 GMT";
} } }
</script>
</head><body onload="putCookie()">
<h1><a href="page2.html">Click here now>>></a></h1>
</body></html>

Eag|e
03-06-2003, 07:35 PM
When I load the page it gives me an error saying: 'document.cookie.value' is null or not an object

Jona
03-06-2003, 09:51 PM
Oh darn, you must be using a different browser. It works in IE6+. What browser are you using?

Eag|e
03-06-2003, 11:11 PM
I'm using IE6.0.2600.0 I'm not sure if I'm even doing it right. Art Of Camoflauge (http://www.ghillie.vze.com) <---the URL for the site. The first page that loads is Page1.html then once you enter your name and click on the link it opens up Index.html which loads the frames basically and the body is sposed to be Page2.html Let me know if I'm even doing it right. THanks - Eag|e

Jona
03-06-2003, 11:31 PM
Change this code:


<script>
cookie_name = "yourName";
var YourName;
function putCookie() {
if(document.cookie != document.cookie) {index = document.cookie.indexOf(cookie_name);}
else { index = -1;}
if (index == -1){ YourName=prompt("Please Enter Your Name To Proceed","");
document.cookie=cookie_name+"="+YourName+"; expires=Monday, 04-Apr-2004 05:00:00 GMT";
} }
</script>


to this code:


<script>
cookie_name = "yourName";
var YourName;
function putCookie() {
if(document.cookie != document.cookie) {index = document.cookie.indexOf(cookie_name);}
else { index = -1;}
if (index == -1){
if(document.cookie.value.indexOf(cookie_name)){ } else { YourName=prompt("What's your name?","");
document.cookie=cookie_name+"="+YourName+"; expires=Monday, 04-Apr-2004 05:00:00 GMT";
} } }
</script>


That should work. That's the code I posted, you need to update it. Just switch out the two scripts.

Nedals
03-07-2003, 01:34 AM
<script>
cookie_name = "yourName";
var YourName;
function putCookie() {
if(document.cookie != document.cookie) {index = document.cookie.indexOf(cookie_name);}
else { index = -1;}
if (index == -1){
if(document.cookie.value.indexOf(cookie_name)){ } else { YourName=prompt("What's your name?","");
document.cookie=cookie_name+"="+YourName+"; expires=Monday, 04-Apr-2004 05:00:00 GMT";
} } }
</script>
Jona, I love your posts but....
if(document.cookie != document.cookie)...
will always return false hence index will always = -1

if(document.cookie.value.indexOf(cookie_name)){ } else {
is better written
if(!document.cookie.value.indexOf(cookie_name)){....

so your script actually does this...
<script>
cookie_name = "yourName";
var YourName;
function putCookie() {
if(!document.cookie.value.indexOf(cookie_name)){
YourName=prompt("What's your name?","");
document.cookie=cookie_name+"="+YourName+"; expires=Monday, 04-Apr-2004 05:00:00 GMT";
}
}
</script>
Just tying to help!!
:)

Eag|e
03-07-2003, 08:57 AM
Ok, it is placing the cookie, but still giving me the error. When I load it, it loads to Page1.html and gives me an error, but when I click on Proceed TO Main Site it has my name on there meaning that it is placing the cookie and loading it on Page2.html (my RightCol.html) but I want to get rid of that error :/

Jona
03-07-2003, 02:34 PM
Eag|e, I don't know what your problem is. I never got an error, and the script works fine.

Thank you, nedals, for tweaking the code. I always forget about the exclamation point stuff... I'll remember next time. Thanks for the heads-up!

Nedals
03-07-2003, 05:42 PM
eag|e,
I've done a rewite of the code jona sent (jona, no offense) that works a little differently.

From your post I gather that what you really want is to have a user enter a name the first time and be redirected to your 'home' page. There-after, each time they enter the site they will go directly to your 'home page'. The script jona gave you will not quite do that. What do you want to do if no name is entered?? Your script writes a cookie with no username and proceeds to the 'home' page!! I've included a fix for that.

This goes in you intro page...

<html><head><title>Fun with cookies</title>
<script type="text/javascript">
<!--
var cookie_name = "userName";
var thisName;
var proceed = false;
// Check that the cookie string contains 'userName' cookie and, if not there, prompt for a name
if (document.cookie.indexOf(cookie_name) == -1) {
while (!thisName) { // loop until valid name or cancel (leaving user on this entry page)
thisName = prompt("Please Enter Your Name To Proceed","");
if (thisName == null) break; // exit loop on cancel
if (thisName) { // Valid name so create a cookie. escape to encode unusual chars
document.cookie = cookie_name +"="+ escape(thisName) +"; expires=Monday, 04-Apr-2004 05:00:00 GMT";
proceed = true;
}
}
} else { proceed = true; } // Cookie already set so proceed directly to index page

// You may need to add the full path instead of just 'index.html' for this to work.
if (proceed) { location.href = "index.html"; }// Redirect to http://www.gamingshack.net/ghillie/index.html
//-->
</script>
</head>
<body bgcolor="#ffffff">
<!--This can be blank or have some intro stuff. You do not need the 'proceed' link,
but if you want it delete the re-direct (last) line in the script above. However, if you do this, users will be able to access
your index page without entering a name-->
</body></html>


This goes in your frame page...

<html><head><title>Frame in index page</title></head>
<body bgcolor="#ffffff">
<script type="text/javascript">
<!--
var cookie_name = "userName";
var thisName;
// if there is a cookie (which there should be), extract the user's name
if (document.cookie.indexOf(cookie_name) > -1) {
var usercookie = document.cookie.split(";")[0]; // Explorer adds a 'style=null' cookie. So dump it!
thisName = unescape(usercookie.split("=")[1]); // Extract the username from the cookie. Use unescape to put back unusual chars
} else { thisName = '"Unregistered Guest"' }
document.write("Welcome " + thisName +" to Art Of Camoflauge! Please feel free to check out everything on the website, it is still under construction so please be patient! Thank you for visiting Art Of Camoflauge and for your patience! See you around " + thisName + "!");

// you can add this to delete a cookie while you are testing.
if (confirm("delete this cookie: " +document.cookie)) {
document.cookie = "userName="+ escape('test')+"; expires=Wednesday, 01-Jan-2003 05:00:00 GMT";
}
//-->
</script>
</body></html>

It should work. I tested it in IE6, NS4, NS7 and it took a hell of a lot longer than I meant to spend on it. :)

The formatting is a little messy but if I include 'code' tags the post will be 3 pages wide.

Jona
03-07-2003, 05:44 PM
Nedals, you've impressed me. It only took me about 5 minutes to whip up that code (my original one), but I guess I misunderstood his question. Oh well.

Eag|e
03-07-2003, 06:09 PM
Nice try on helping me Jona, thanks for trying though. Nedals, that works good but I want to get rid of the confirm box that asks to delete it or keep it so they don't have to mess with that everytime. What part of the code would I take out to make it so it doesn't come up? Thanks guys.

Jona
03-07-2003, 06:14 PM
Taken from Nedals' code:

This goes in you intro page...

<html><head><title>Fun with cookies</title>
<script type="text/javascript">
<!--
var cookie_name = "userName";
var thisName;
var proceed = false;
// Check that the cookie string contains 'userName' cookie and, if not there, prompt for a name
if (document.cookie.indexOf(cookie_name) == -1) {
while (!thisName) { // loop until valid name or cancel (leaving user on this entry page)
thisName = prompt("Please Enter Your Name To Proceed","");
if (thisName == null) break; // exit loop on cancel
if (thisName) { // Valid name so create a cookie. escape to encode unusual chars
document.cookie = cookie_name +"="+ escape(thisName) +"; expires=Monday, 04-Apr-2004 05:00:00 GMT";
proceed = true;
}
}
} else { proceed = true; } // Cookie already set so proceed directly to index page

// You may need to add the full path instead of just 'index.html' for this to work.
if (proceed) { location.href = "index.html"; }// Redirect to http://www.gamingshack.net/ghillie/index.html
//-->
</script>
</head>
<body bgcolor="#ffffff">
<!--This can be blank or have some intro stuff. You do not need the 'proceed' link,
but if you want it delete the re-direct (last) line in the script above. However, if you do this, users will be able to access
your index page without entering a name-->
</body></html>


This goes in your frame page...

<html><head><title>Frame in index page</title></head>
<body bgcolor="#ffffff">
<script type="text/javascript">
<!--
var cookie_name = "userName";
var thisName;
// if there is a cookie (which there should be), extract the user's name
if (document.cookie.indexOf(cookie_name) > -1) {
var usercookie = document.cookie.split(";")[0]; // Explorer adds a 'style=null' cookie. So dump it!
thisName = unescape(usercookie.split("=")[1]); // Extract the username from the cookie. Use unescape to put back unusual chars
} else { thisName = '"Unregistered Guest"' }
document.write("Welcome " + thisName +" to Art Of Camoflauge! Please feel free to check out everything on the website, it is still under construction so please be patient! Thank you for visiting Art Of Camoflauge and for your patience! See you around " + thisName + "!");
//-->
</script>
</body></html>

I just took out this part:

// you can add this to delete a cookie while you are testing.
if (confirm("delete this cookie: " +document.cookie)) {
document.cookie = "userName="+ escape('test')+"; expires=Wednesday, 01-Jan-2003 05:00:00 GMT";
}

Nedals
03-07-2003, 06:24 PM
Thanks jona.
eag|e, you must be REALLY new at this.
Read and learn.
and, as jona says... programming = fun

Eag|e
03-07-2003, 07:30 PM
Lol, I just started learning JS, but I'm almost in Programming 3 (last Visual Basic 6.0 class at my school) but JS is somewat confusing.Oh well, I just need to work with it ALOT more. Recommend any good JS books?

Jona
03-07-2003, 07:48 PM
Yah, JS book? Got it. Here's a great suggestion: ANY JS BOOK YOU CAN GET YOUR HANDS ON!!!

I personally can't recommend anything. It's always been a problem for me to find a good JS book that's useful. My first JS book was Advanced JavaScript Insights and Innovative Techniques by Dan Livingston. Of course, that'd be waaaaaayy too advanced for you since you're just starting JS, but it was (and still is!) my first JS book.

I think the JS Bible is good, but then, so is JavaScript Unleashed by Sams Publishing. I always wanted that book, but couldn't afford it so I got a cheaper book, which was/is my first one.

Eag|e
03-07-2003, 11:04 PM
Ok Jona, thanks. And thank you all for your help. Peace - Eag|
e

Eag|e
03-08-2003, 01:04 AM
nedals, I just uploaded it to the website, and it asks you to enter your name, and you do and hit enter and then it doesn't load the page. It worked when I didn't have it uploaded it and I did it right from the main file, but when I put it on the net it didn't work. Art Of Camoflauge (http://www.ghillie.vze.com) See if it's workin for you.

Nedals
03-08-2003, 02:13 AM
You recall this...
// You may need to add the full path instead of just 'index.html' for this to work.
if (proceed) { location.href = "index.html"; }// Redirect to http://www.gamingshack.net/ghillie/index.html

My guess was right... Change to

// You may need to add the full path instead of just 'index.html' for this to work.
if (proceed) { location.href = "http://www.gamingshack.net/ghillie/index.html"; }

Eag|e
03-08-2003, 10:02 AM
File paths are one thing I hate about websites...they work when you got em on your computer but then once they are on the net sometimes they don't work. Ok, well thanks guys. - Eag|e

Nedals
03-08-2003, 11:33 AM
Paths on a website a pretty straight forward..
When in doubt, start with the domain name including the 'http://...'

I noticed you are now able to pull up the right page after the name is entered but my guess is that the cookie is not working. Right?
The problem is that you are changing domains and cookies don't like to change domains. So you are going to have to mess around with your code...

One IMPORTANT note here...
1. Programming is 1/3 planning, 1/3 coding, and 1/3 DEBUGGING. (so don't loose hope!)
2. Comment the hell out of your code. Especially when just starting out.

Given that..
Remember that bit of code you wanted out. Well, put it back in until the site is FULLY working
In addition, find this code on the intro page...

if (thisName == null) break; // exit loop on cancel

and change it to this......

if (thisName == null) {
// you can add this to delete a cookie WHILE YOU ARE TESTING.
if (confirm("delete this cookie: " +document.cookie)) {
document.cookie = "userName="+ escape('test')+"; expires=Wednesday, 01-Jan-2003 05:00:00 GMT";
break; // exit loop on cancel
}

Also, find this in your intro page.....

document.cookie = cookie_name +"="+ escape(thisName) +"; expires=Monday, 04-Apr-2004 05:00:00 GMT";

and add....

document.cookie = cookie_name +"="+ escape(thisName) +"; expires=Monday, 04-Apr-2004 05:00:00 GMT; path=/";

If this doesn't work, I am going to need help from another member (please) who knows how to get cookies to work across domains.
If it cannot be done, which I suspect is true, then you are going to need a whole new approach that doesn't use cookies.

Eag|e
03-08-2003, 11:52 AM
It's not working for you? Or is that just something I should add. Because it is working for me now and everyone I gave it to.

Nedals
03-08-2003, 12:15 PM
Originally posted by Eag|e
It's not working for you? Or is that just something I should add. Because it is working for me now and everyone I gave it to.
It going to the index page but the name is 'Unregistered Guest', which means the cookie isn't working. If it's working correctly for others, then I don't know what my problem is! So forget my previuos post.