Ken Ward's Java Script Tutorial ...

Cookies 5

Getting the user's name and remembering it when they next visit.

If you entered your name, then you get a welcome above. If you return to this page, it will remember you and greet you with your name.

The code in the BODY above is:

<script type="text/javascript">

<!--//

NewNamer()

//-->

</script>

That is, it calls a function called NewNamer. This function, in the HEAD of the document has the following code:

function NewNamer(){

var now= new Date();

now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);

var username = GetCookie("username");

if ((!username)||(username=='null'))

{

username = prompt("Please enter your name:", "");

}

setCookie("username", username, now);

if (username)

{

document.write("Welcome to the page, "+username+

". If you are not "+username+

", then <a href=\"cookie5.htm\" onClick=\""+

ChangeName();history.go(-1)\">click here.</a>");

setCookie("username", username, now);

}

else

document.write("Welcome to the page, (No name given) "+

"<a href=\"cookie5.htm\" onClick="+

"\"ChangeName();history.go(-1)\">Click here </a>to add your name.");

}

The new function writes code so that users can change the name they are called. It calls a function called ChangeName. The code is:

function ChangeName(){

var username = GetCookie("username");

username = prompt("Please enter your name:", "");

setCookie("username", username);

NewNamer()}

This function raises a prompt and then calls NewNamer to update the page, etc.

The jsEditor contains a cookie wizard to put the basic code into your page. You can modify this code to create the effects you want. In addition to its many other features, you can use the jsEditor's knowledge base to explore the Math object. You can order the jsEditor online and download the full version immediately, so in a few minutes you could be using it to help you learn and develop scripts.

Our next function will get the number of times the user has visited our page.

 

Ken Ward's HTML Guide...

And for some programming for your brain: check out New Life Course