8. Validate new registration
Create the following function to add the user's first name to the cookie:
function addcookie(name,value)
{
var expiredate = new Date();
var expires = "";
expiredate.setTime(expiredate.getTime()
+ (1000*60*60*24*365));
expires = "expires="
+ expiredate.toGMTString();
document.cookie = name +
"=" + escape(value) + ";" + expires + ";";
}
Insert the following code in the chkentry() function:
function chkentry()
…
else
{
addcookie("FirstName",
document.Registration.FirstName.value);
alert("Thank you!");
…
Create the following function to retrieve the cookie in welcome.html:
function getcookie(name)
{
var mycookie = document.cookie
+ ";";
var value = null;
var findName = name +
"=";
var end;
if (mycookie.length > 0)
{
var begin =
mycookie.indexOf(findName);
if (begin != -1)
{
begin = begin +
findName.length;
end =
mycookie.indexOf(";", begin);
if (end == -1)
end = mycookie.length;
value =
unescape(mycookie.substring(begin, end));
}
}
return value;
}
Create the following function to check whether registration is needed and place it in welcome.html:
function chkcookie()
{
var name = "";
name = getcookie("FirstName");
if (name != null)
alert("Welcome " +
name);
else
location="form.html";
}
Insert the onLoad property in the <BODY> tag of welcome.html
<BODY onLoad="chkcookie()">