Next, you will add the code to create the form and write the cookie. To see what the page will look like when you are done, click here.
The code is given below.
<HTML> <HEAD> <TITLE>Cookie Monster!</TITLE> <SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT"> <!-- Hide script from older browsers alert("This page uses cookies!") //The lines below set the expiration date for the cookie expireDate= new Date expireDate.setMonth(expireDate.getMonth()+6) //This line initialized userName to NULL (nothing) userName="" // != means NOT EQUAL, so if there was a previous value //for userName, we read it in from the cookie file if(document.cookie!="") { userName=document.cookie.split("=")[1] } //the code below writes the cookie function setCookie(){ userName=document.myForm.nameField.value document.cookie="userName="+userName+";expires=" +expireDate.toGMTString() } //end hiding script --> </SCRIPT> </HEAD> <BODY BGCOLOR="WHITE" onLoad="document.myForm.nameField.value=userName"> <center><h2>A Simple Cookie created with JavaScript</h2></center> <P> <B>Privacy Notice:</B> Information entered on this page will be stored in a cookie that will persist for 6 months. This is a class assignment for IT 108. <P><P> <FORM NAME="myForm"> <!--This creates the form--> <H3>Enter your name: <INPUT TYPE=TEXT NAME="nameField" onBlur="setCookie()"></H3> <!--the cookie is written when you leave the form--> </FORM> <P><P> This page &#169 2002 by Your Name </BODY> </HTML>
Click
here to return to the cookie lab
Click here to
return to the IT 108 main page