Jump to content

Initialization


Stopofeger

Recommended Posts

Hi there,

One thing i still dont understand about javascript is exctly when all the objects are initialized.

for example if i try to access form elements from the head tags.
the objects are still undefined and error comes saying the object is null.
so exactly where shud i place my js so the element objects gets ready.
Link to comment
https://forums.phpfreaks.com/topic/31755-initialization/
Share on other sites

You will want to add an onLoad event handler to call your JavaScript when the page is fully loaded, otherwise your JavaScript will be run before the the rest of the web page is loaded and thus you get the error messages you are getting. Example:

[code]<html>
<head>
<script type="text/javascript">
/* this function will be called when the page is loaded */
function load()
{
    alert("Page is loaded");
}
</script>
</head>
<!-- The event handler: -->
<body onload="load()">

<h1>Page content</h1>

page content here. page content here. page content here. page content here.
page content here. page content here. page content here. page content here.
<br /></br />
page content here. page content here. page content here. page content here.
page content here. page content here. page content here. page content here.
page content here. page content here. page content here. page content here.
page content here. page content here. page content here. page content here.
<br /><br />
page content here. page content here. page content here. page content here.
page content here. page content here.

</body>

</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/31755-initialization/#findComment-147296
Share on other sites

Yes thats what i do.
But there is sometimes when i have to do things before the document is fully loaded.
So my question is, which objects can i use before the page loads?
definetly not document object, its null.
that leaves me with only predefined objects like Array, Date etc. Btw can i use window obj before loading?
Link to comment
https://forums.phpfreaks.com/topic/31755-initialization/#findComment-147828
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.