PDA

View Full Version : "Object Expected" error.


doni
03-08-2006, 03:36 AM
IE6 is giving me an "Object Expected" error. IE tells me the line number & I've marked that line in BOLD in the code below. It may not look like much--but that's the point. I've been having this error all night and I've been removing things trying to boil it down to bare bones to figure out what's going on.



<head>
<script language="JavaScript">
<!--
function test(){
document.write "Hello"
}
//-->
</script>
</head>
<body onload=test()>

test
</body>

_Aerospace_Eng_
03-08-2006, 03:46 AM
You need parentheses around "Hello"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
<!--
function test(){
document.write("Hello");
}
window.onload = test;
//-->
</script>
</head>

<body>
</body>
</html>

Also its best to avoid putting any kind of event handler in the body tag. Most if not all can go in the script tags. Also document.write will overwrite everything on the page.

glenngv
03-08-2006, 03:48 AM
document.write("Hello")If you run that page in Firefox and open the Javascript Console, you will see a more descriptive error message.

doni
03-08-2006, 04:07 PM
Thanks.

I guess I've been doing too much PHP. In PHP that would have been written as Echo "Test";.

I really thought it was something with the "onload" function call.

Once I added the parens it worked--but that was the stripped down version, when I put my own code back in, I knew not to focus on the onload statement.

I eventually tracked it down to a missing semi-colon after a variable definition once I wasn't so focused on the onload statement.