Hi.
I'm working on learning about cookies. I've got a very short sample script given to me in class and I found the same script in a Javascript book (I guess I figured out what the teacher's been reading).
When I open this script in IE, I get a Javascript error that 'userName is undefined' at line 23.
Here's a link to the page where you can try it out...
www.detailsfree.com/cscd378/cookie.html
Here's the code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cookie</title>
<script language="Javascript" type="text/javascript">
<!-- Hide script from older browers
expireDate = new Date();
expireDate.setMonth(expireDate.getMonth()+6);
userName = "";
if (document.cookie != "" {
userName = document.cookie.split("=")[1];
} // end if
function setCookie() {
userName = document.myForm.nameField.value;
document.cookie = "userName=" + userName + "; expires=" + expireDate.toGMTString();
} // end function
//-->
</script>
</head>
<body bgcolor="#FFFFFF" onload="document.myForm.nameField.value = userName">
<form name="myForm" action="#">
<h1>Enter your name: <input type="text" name="nameField" onblur="setCookie()" /></h1>
</form>
</body>
</html>
I'd appreciate any feedback on this. Thanks.
David