Click button on load event

11,846
<body onload='onLoad(); fan();'>...

However inline JS is best avoided and you would do well to begin looking into centralised event management. There are various advantages to this.

An answer I wrote yesterday to another question outlines why this is. Something like jQuery makes this trivial if it's new for you.

$(function() {
    $('body').on('load', function() {
        onLoad();
        fan();
    });
});
Share:
11,846
Ebikeneser
Author by

Ebikeneser

C# &amp; VB Programmer.

Updated on June 04, 2022

Comments

  • Ebikeneser
    Ebikeneser about 2 years

    I have the following javascript -

    function onLoad() {
        if (!(document.applets && document.VLVChart && document.VLVChart.isActive())) {
            setTimeout('onLoad()', 200);
            return;
        }
        objChart = document.VLVChart;
        PollEvent();
    }
    
    function fan() {
        objChart.reorganize();
    }
    

    And then when the HTML page is loaded -

    <body onLoad="onLoad()">
    

    and have a button within the HTML that execute the fan() function -

    <input type='button' value='Fan' onClick='fan();'>
    

    Is it possible for me to activate the fan() function within the onload event so that a user does ont have to click the button?

    EDIT After trying the provided answers, on debugging the code breaks on the line -

    objChart.reorganize();
    

    Within the fan() function with the error -

    SCRIPT5007: Unable to get value of the property 'reorganize': object is null or undefined 
    

    This is odd as when I manually click the button on the page, the function works fine.

    Solution After much head scratching I have realised that I was trying to load the fan() function before the page (and more specifically the objChart) had fully loaded. Hence why adding the function in the onLoad event was not working. I added a setTimeout -

    function Fan()
    {
    setTimeout(function(){objChart.reorganize();},3000);
    }