call js after page loads

U

uNConVeNtiOnAL

Hi -

I can't call my js until the page is loaded down to the </body> tag - is
there
something similar to body onload that I can use to automatically run the
script
just before the page is done loading ???

Thanks

Tom
 
S

Stephen

uNConVeNtiOnAL said:
Hi -

I can't call my js until the page is loaded down to the </body> tag - is
there
something similar to body onload that I can use to automatically run the
script
just before the page is done loading ???

Thanks

Tom

Just put <script></script> at whatever point you want the code to run.
Of course, you have to put some valid js between the <script>s :).
Regards
Stephen

E.G.,

<html><head><title>Run before I finish Loading</title>
<script type="text/javascript">
alert ("I'm running before we finished loading!");
</script>
</head>
<body onload="alert('Now I\'m through loading!');">
<h1>Hello World</h1>
</body>
</html>
 
G

George Ziniewicz

uNConVeNtiOnAL said:
Hi -

I can't call my js until the page is loaded down to the </body> tag - is
there
something similar to body onload that I can use to automatically run the
script
just before the page is done loading ???

My observations as a somewhat newbie:

Inline code before <body> gets executed as it is read.

<body onload="..."> code gets executed allegedly after the page is
loaded, but it hasn't allowed me to refer to body elements further down in
body, like later div id's, widget object's onclick handler setup when the
widget is self-generated via doc.writes, etc. (IE and Mozilla?)

Code just before </body> wrapped in a <script></script> pair is the best
place I've found to have code that works on all types of objects since
everything has been defined before "here", it is here I often call an init()
routine (if it doesn't create any new html widgets w/ doc/write which should
be done exactly where they need to appear in body).

The main thing I learned is that the html & javascript is read and
processed once, from top down, so all online code can only refer to elements
it has read beforehand. Only functions that are called later (event
handlers) can be placed anywhere and refer to anything, with the hope that
they are not activated before a slow loading page finishes!


zin -- http://www.zintel.com
 
D

Dr John Stockton

JRS: In article <gUHbb.6714$gv5.96@fed1read05>, seen in
news:comp.lang.javascript said:
The main thing I learned is that the html & javascript is read and
processed once, from top down, so all online code can only refer to elements
it has read beforehand. Only functions that are called later (event
handlers) can be placed anywhere and refer to anything, with the hope that
they are not activated before a slow loading page finishes!


You could, I believe, put at the beginning of the first script var U=0
and at the very end of the page U=1

Then your event handlers could each begin with

if (TS()) return false

with a

function TS() {
if (U=0) alert('Too Hasty')
return U=0 }

or similar. Untested.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top