Ken Ward's Java Script Tutorial ...

Cookies 4

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

Welcome to the page, .

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 is:

<p>Welcome to the page, <script type="text/javascript">

<!--//

MyNamer()

//-->

</script>. </p>

That is, we call a new function called MyNamer(). This function, which is in the HEAD is:

function MyNamer(){

//First we set the cookie date to a year hence.

var now= new Date();

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

//We call the function GetCookie() with the name, "username".

var username = GetCookie("username");

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

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

setCookie("username", username, now);

if (username) {

document.write(username);

setCookie("username", username, now);

}

else

document.write("You didn\'t enter your name.<a href=# onClick=\"MyNamer();history.go(-1)\">Enter it now?</a>");

}

So far, if the user does not enter their name, there are no more options. Or if the name is wrong, they have to live with being called something else.

Let's see what we can do to correct matters.

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.

 


Ken Ward's HTML Guide...