Register Your Name with the Cookie-Meister

Please enter your name. When you return to this page within a year, you will be greeted with a personalized greeting.

Enter your name:  



<SCRIPT>

function setCookie(name, value, expires) {
     document.cookie = name + "=" + escape(value) +
                                    ((expires) ? "; expires=" + expires.toGMTString() : "");
}

function getCookie(name) {
     var prefix = name + "=";
     var begin = document.cookie.indexOf(prefix);

     if (begin == -1) return null;

     var end = document.cookie.indexOf(";", begin);

     if (end == -1) end = document.cookie.length;

     return unescape(document.cookie.substring(begin + prefix.length, end));
}

function register(name) {
     var expiration = new Date();

     expiration.setYear(expiration.getYear() + 1);

     setCookie("TheCoolJavaScriptPage", name, expiration);
}

</SCRIPT>

<H1>Register Your Name with the Cookie-Meister</H1>

<SCRIPT>

document.write(document.cookie);

var yourname = getCookie("TheCoolJavaScriptPage");

if (yourname) document.write("Welcome Back, ", yourname);
else document.write("You haven't been here in the last year...");

</SCRIPT>

Enter your name. When you return to this page within a year, you will be greeted with a personalized greeting.

<FORM onSubmit="return false">
     Enter your name: <INPUT TYPE="text" NAME="username">
     <INPUT TYPE="button" value="Register"
      
onClick="register(this.form.username.value); history.go(0)">
</FORM>