Link to home
Start Free TrialLog in
Avatar of cofcmgr
cofcmgr

asked on

javascript question on converting body onload to window.onload

a quick one.....how do i convert this body onload to a window.onload?  

<body onload="nextCount('counter');">

thought it would be easy enough, but getting hung up on how to pass the "counter" parameter?
Avatar of frindo
frindo

just add the following between the script tags at the top of the page:

window.onload=nextCount('counter');

Also, there is no functional difference between the two methods so the only reason you would convert from a body onload to a window.onload is for viewing purposes (you don't want to mix your javascript with your HTML)
ASKER CERTIFIED SOLUTION
Avatar of Kiran Paul VJ
Kiran Paul VJ
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@frindo that is not correct.

eventhandler = function
or
eventhandler = something returning a function

In you case, the nextcount is evaluated immediately and the window.onload set to the result of the evaluation.
KiranVJ has the correct version
Oh, my mistake.

So could you just put "nextCount('counter');" at the top of the page and it would just as well?
Agreed with KiranVJ solution
No, you could put it at the very bottom of the page in a script before </body> and it would MORE OR LESS work the same.

onLoad is triggered in IE at least more or less when the page and its content has loaded. It might trigger well after the end body tag has rendered but at least the html and dom objects on the page are there

The best is the function assigned to window.onload either directly

function initPage() {
  nextCount('counter');
}
window.onload=initPage; // notice the lack of ()

or as already suggested, anonymously :
window.onload = function() {
  nextCount('counter');
}

points to Kiran
hi mplungjan,

1 doubt i just got.

window.onload = function() {
  functionA('parameter');
}

window.onload = function() {
  functionB('someOther_parameter');
}

will the 2 functions be executed or just the second one.

kiranvj
Just the second one

You need



window.onload = function() {
  functionA('parameter');
  functionB('someOther_parameter');
}

thats good, Thanks