Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

window.onload() ??? Why doesn't it work?

Status
Not open for further replies.

Dgandhi

Programmer
Oct 5, 2004
11
0
0
IN

Why does'nt this work?
<BODY>
<SCRIPT language=&quot;JavaScript&quot;>
window.onLoad=alert(&quot;onLoad() called.&quot;);
window.onUnload=alert(&quot;onUnload() called.&quot;);
alert(&quot;In Body&quot;);
</SCRIPT>
</BODY>

I would have expected the &quot;In Body&quot; alert to appear first, then the &quot;onLoad&quot; alert and nothing else. When I exit the window, I should get the &quot;onUnload&quot; alert. Instead, the 3 alert()s appear in the order in which they appear in this code. If I put these methods in the <BODY> tag, they get invoked as expected (i.e., <BODY onLoad=&quot;alert('onLoad() called.');&quot; ...> ). BTW, it doesn't work on NS or IE.
 
you must sepcify a function reference for those event handlers. you are just telling it to alert something, which is returning true, and thus no error is being created. what you need to do is this:

<SCRIPT language=&quot;JavaScript&quot;>
function winLoad()
{
alert(&quot;window loaded&quot;)
}

function winDone()
{
alert(&quot;window unloaded&quot;)
}

window.onload=winLoad
window.onunload=winDone

</SCRIPT>

notice in the event handler declarations all the event handler letters are lowercase and the function references they equal do not have ()'s after them jared@aauser.com
 
Many thanks! I guess, there is no way to pass arguments to these load/unload functions. e.g., startPoll(3000);
 
im not sure, I haven't found a way. the only thing I came up with is a work around:

function callstartPoll()
{
startpoll(1,2,n)
}

window.onload=callstartPoll jared@aauser.com
 
I am actually writing a custom JSP TAG to generate the code so I ended up doing something like:

a = 5;
b = 6;
window.onload=startpoll();

Then, in startpoll(), I pick up these global variables. Not elegant, but I couldn't find any other way. By the time I'm processing the tag, I'm in the body.
 
it should say:

window.onload=startpoll;

without ()s jared@aauser.com
 
No, it shouldn't. The correct syntax for calling a function is function();

The reason I said to put the code in the header is because if you put window.onload in the body, it tries to process it in the body. However, the entire page hasn't finished loading yet so it ignores it. If you put it in the header or in the <body> tag, that seems to work more consistently because it actually processes it onload rather than during load.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Yes it should. Event handlers like window.onload expect a reference to the function, not the function running, Tom. jared@aauser.com
 
try this code tom, its kinda funny

<script>
function doUnload()
{
alert(&quot;Exiting Page&quot;)
}
window.onunload=doUnload()
</script> jared@aauser.com
 
I see your point. However, that is actually a wrong response. Event handlers are supposed to call the function, at least according to
There, they give this example:
Code:
<SCRIPT>
var whichImage=0
var maxImages=5
function changeAnimation(theImage) 
{
   ++whichImage
   if (whichImage <= maxImages) {
      var imageName=&quot;!anim&quot; + whichImage + &quot;.gif&quot;
      theImage.src=imageName
   } else {
      whichImage=-1
      return
   }
}
</SCRIPT>
<IMG NAME=&quot;changingAnimation&quot; SRC=&quot;!anim0.gif&quot; BORDER=0 ALIGN=&quot;top&quot; onLoad=&quot;changeAnimation(this)&quot;>
How else would you pass parameters? Well, I suppose you wouldn't have to if the event itself (this) is implied. Isn't there a case when you would want to pass something else though?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Tom,

Yes, your last post is correct. However it is not the same thing as assigning a handler to an object's event property which is what was being discussed earlier, i.e.:

window.onload = myfunc;

There is a distinct difference in placing a function in an HTML elements event handler attribute vs. working with an 'object' in a script block.

Hope this helps
-pete
 
Netscape deals with them as one in the same. It does not distinguish between <img onLoad=&quot;function(params)&quot;> and document.images[1].onload=function(params); Are NS and IE different in that regard? Does NS documentation agree with its browser?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Tom,

My bad, I am wrong. This works in IE 5 on NT 4 Workstation

function window_onload( s) {
alert(&quot;window_onload: &quot; + s);
}

//-->
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=javascript>
<!--
document.body.onload = window_onload(&quot;home&quot;);
//-->
</SCRIPT>

Thanks for watching my back Tom :)
-pete
 
actually, that doesn't work... imediately after it executes it gives an error, saying not implemented. it does the same thing if you use

document.body.onunload = window_onload(&quot;home&quot;);


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top