spacer
Yehuda Shiran February 20, 2000
A Name Tracker
Tips: February 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

The following script asks the user for his or her name, and "remembers" the input. It then welcomes the user each time he or she accesses the page, without asking again for the name. It's useful to provide a button, or another mechanism, to let the user change the name (in case several people are using the same machine, or in case the user has mistyped his or her name).

<SCRIPT LANGUAGE="JavaScript">
<!--

// create an instance of the Date object
var now = new Date();
// fix the bug in Navigator 2.0, Macintosh
fixDate(now);
// cookie expires in one year (actually, 365 days)
// 365 days in a year
// 24 hours in a day
// 60 minutes in an hour
// 60 seconds in a minute
// 1000 milliseconds in a second
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
var username = getCookie("username");
// if the cookie was not found, ask for the name
if (!username) {
  username = prompt("Please enter your name:", "");
}
// set the new cookie
setCookie("username", username, now);
if (username) {
  document.write("Welcome to this page, " + username + ".");
  setCookie("username", username, now);
} else
  document.write("You refused to enter your name.");
// -->
</SCRIPT>

Note that this script can be placed anywhere on the page. It welcomes the user by printing some text where you placed the script.

Learn more about cookies in Column 8, Crispy JavaScript Cookies.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business