How to combine two window.onload?

I have two window.onload in one page, one of them can not run well


<script>
function aaa(...)
...

window.onload=function(){
var menucount...;
if ...;}
}
</script>
<body onload="menu.list('div2')">

I have read some tutorial like:

function start() {
  func1();
  func2();
}
window.onload = start;

But I still don’t know how to use in my code. one in <script>, another in <body>. So is there anyone can help me? Thanks.

This is a JavaScript question but window.onload is not reliable, body onload is a better bet and you can call several onload events within the body tag.

try to call the function at the end of your webpage.

For exammple:


</body>
</html>
<script>
window.onload = start();
</script>

Couldn’t he also simply do this as well?

<script>
function start()
{
func1();
func2();

}

function func1()
{
document.write("Hellow World!");
}
function func2()
{
document.write("\
Hello World 2!");
}

window.onload = start;

</script>

Works fine for me.

Thanks for a quickly reply.
I tried to add
<script>
window.onload = start();
</script>
after </html>, but it not work. Is there need modify other codes?

Can you put my code in it for me? I don’t whether should modify the code in <body>? Thanks.

For me to help you, I need to see your code. Post the whole code and I’ll set it up for you.

:nono:

Tag event handlers should not be used. Ever.

I’ll repeat that.

Tag event handlers should not be used. Ever.

They pollute the HTML with javascript code, which is everybit as bad as putting business logic PHP into a template file - maybe even worse because the PHP has the good grace to disappear once the template eval’s, and the javascript cannot escape.

Events should be registered to the window onload event, but rather than worry about the stupidity that is the world of 101 browsers, delegate the problem to a Javascript framework.

In Prototype.js


Event.observe( window, 'load', function() {
  // My window event code goes here.
} );

In jQuery


$(document).ready(function() {
  // Handler for .ready() called.
});

Both of these have an important advantage over the onload event handler - they don’t fire until all the objects of the page have loaded, which may be important if you have multiple scripts or lengthy CSS to process. You can also attach as many events as possible.

The onX handlers though should be treated as deprecated. They create far more problems than they solve.

Now that said, reporting original post to have it moved to the javascript forum. This has nothing whatsoever to do with PHP.


<script type="text/javascript">
//Set the id names of your tablink (without a number at the end)
var tablink_idname = new Array("tablink")
//Set the id name of your tabcontentarea (without a number at the end)
var tabcontent_idname = new Array("tabcontent") 
//Set the number of your tabs
var tabcount = new Array("3")
//Set the Tab wich should load at start (In this Example:Tab 2 visible on load)
var loadtabs = new Array("1")  
//Set the Number of the Menu which should autochange (if you dont't want to have a change menu set it to 0)
var autochangemenu = 0;
//the speed in seconds when the tabs should change
var changespeed = 10;
//should the autochange stop if the user hover over a tab from the autochangemenu? 0=no 1=yes
var stoponhover = 0;
//END MENU SETTINGS

/*Swich EasyTabs Functions - no need to edit something here*/
function easytabs(menunr, active) {if (menunr == autochangemenu){currenttab=active;}if ((menunr == autochangemenu)&&(stoponhover==1)) {stop_autochange()} else if ((menunr == autochangemenu)&&(stoponhover==0))  {counter=0;}menunr = menunr-1;for (i=1; i <= tabcount[menunr]; i++){document.getElementById(tablink_idname[menunr]+i).className='tab'+i;document.getElementById(tabcontent_idname[menunr]+i).style.display = 'none';}document.getElementById(tablink_idname[menunr]+active).className='tab'+active+' tabactive';document.getElementById(tabcontent_idname[menunr]+active).style.display = 'block';}var timer; counter=0; var totaltabs=tabcount[autochangemenu-1];var currenttab=loadtabs[autochangemenu-1];function start_autochange(){counter=counter+1;timer=setTimeout("start_autochange()",1000);if (counter == changespeed+1) {currenttab++;if (currenttab>totaltabs) {currenttab=1}easytabs(autochangemenu,currenttab);restart_autochange();}}function restart_autochange(){clearTimeout(timer);counter=0;start_autochange();}function stop_autochange(){clearTimeout(timer);counter=0;}

window.onload=function(){
var menucount=loadtabs.length; var a = 0; var b = 1; do {easytabs(b, loadtabs[a]);  a++; b++;}while (b<=menucount);
if (autochangemenu!=0){start_autochange();}
}
</script>
<body onload="menu.list('div2')">

These are the code in the page.
There still have some js files.
Thanks.

I have solved the problem. Thanks for everyone.