Object required error: When do I call my code?

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I have a page that is giving me the following error message:

Error: Object required

I have determined, after some experimenting, that this is due to the fact
that the HTML has not been parsed when the script is called. Here are two of
the variations of the page:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function SlideIntoView(id)
{window.alert(document.getElementById(id).id);}
SlideIntoView('divContentArea');
</script>
</head>
<body>
<div id="divContentArea"></div>
</body>
</html>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function SlideIntoView(id)
{window.alert(document.getElementById(id).id);}
</script>
</head>
<body onload="SlideIntoView('divContentArea');">
<div id="divContentArea"></div>
</body>
</html>


The only difference between the two pages is that the statement

SlideIntoView('divContentArea');

Is located inside the script tag in the first version, and in the body's
onload eventhandler in the second. The first gives the Object required error
message, but the second works fine. However, with all the code that
sometimes needs called right away, and that gets added dynamically in
server-side development, it would be unrealistic to need to put it all in
onload. I have never needed to worry about this before (I am currently using
IE7). How can I make sure the browser has parsed the HTML before calling the
code? Any help would be appreciated. Thanks.
 
Nathan said:
Is located inside the script tag in the first version, and in the
body's onload eventhandler in the second. The first gives the Object
required error message, but the second works fine. However, with all
the code that sometimes needs called right away, and that gets added
dynamically in server-side development, it would be unrealistic to
need to put it all in onload. I have never needed to worry about this
before (I am currently using IE7). How can I make sure the browser
has parsed the HTML before calling the code?

You have to call that code in response to onload event, no way around it. However, writing onload= attribute is not the only way to
handle onload event. You can do something like

window.attachEvent('onload', function() {DoWhatever();});

You can attach more than one handler this way.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going
to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
 
Bookmark and use these links in the future, please, Nate:

IE Developer Center
http://msdn.microsoft.com/en-us/ie/default.aspx

Learn IE8
http://msdn.microsoft.com/en-us/ie/aa740473.aspx

HTML and DHTML Overviews and Tutorials
http://msdn.microsoft.com/en-us/library/ms537623.aspx and

Cascading Style Sheets (CSS)
http://msdn2.microsoft.com/en-us/ie/aa740476.aspx

Expression Web SuperPreview for Internet Explorer (free, stand-alone visual
debugging tool for IE6, IE7, and IE8)
http://www.microsoft.com/downloads/...FamilyID=8e6ac106-525d-45d0-84db-dccff3fae677

Expression Web SuperPreview Release Notes
http://www.microsoft.com/expression/products/Web_SuperPreviewReleaseNotes.aspx

Validators:
http://validator.w3.org/
http://jigsaw.w3.org/css-validator/

MSDN IE Development Forums <=post here instead!
http://social.msdn.microsoft.com/forums/en-US/category/iedevelopment/
 
Back
Top