Register Your Name with the Cookie-Meister



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

var yourname = getCookie("TheCoolJavaScriptPage")

// If yourname is not null then welcome the user back with "Welcome ...
//
If yourname is null then write out "You haven't ... and also the <FORM>etc...</FORM>

if (yourname) document.write("Welcome Back, ", yourname)
else (yourname == null) {
     document.write("You haven't been here in the last year...")
     document.
write("Please enter your name. When you return to this page within a year,");
     document.
write(" you will be greeted with a personalized greeting.");

     document.write('<FORM ONSUBMIT="return false">');
     document.
write('<P>Enter your name: ");
     document.
write('<INPUT TYPE="text" NAME="username" SIZE="10">');
     document.write('<INPUT TYPE="button" VALUE="Register"');
     document.
write('onClick="register(this.form.username.value); history.go(0)">');
     document.write('</FORM>');
}

</SCRIPT>