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

10-19-2005, 09:02 PM

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">
<script language="Javascript" type="text/javascript">
<!-- Hide script from older browers
expireDate = new Date();

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

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

I'd appreciate any feedback on this. Thanks.


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

I had a missing right parenthesis ')'.