...

View Full Version : Someone to look over code and help me find error (simple newbie problem)



davenet
10-19-2005, 09:02 PM
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:



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

davenet
10-19-2005, 10:57 PM
Found the error.

I had a missing right parenthesis ')'.

Davd



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum