3

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);
}
Ebikeneser
  • 2,582
  • 13
  • 57
  • 111

3 Answers3

3
<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();
    });
});
Community
  • 1
  • 1
Mitya
  • 33,629
  • 9
  • 60
  • 107
  • Its odd, I have tried both your first answer on putting the fan() function into the onload event, and also @feeela calling the fan() function in the onload function. For some reason neither seem work? – Ebikeneser Aug 01 '12 at 09:51
  • Both are definitely sound. The latter requires that you have jQuery loaded into the page. Any console errors? Always check the console with JS issues. If the problem persists, post a JS Fiddle so we can see. – Mitya Aug 01 '12 at 09:53
  • If `reorganize()` is not a function that is a separate issue from the one you asked about. You asked how to invoke `fan()` onload and I provided that. This new issue means something isn't being loaded or declared properly. – Mitya Aug 01 '12 at 10:21
  • I think this is a timing issue, I think I am trying to execute fan before the page has properly loaded... – Ebikeneser Aug 01 '12 at 10:32
0

Reading your question I assume you didn't even have tried. Just call that function from within your onLoad()-function:

function onLoad()
{
    fan();
    /* … */
}
feeela
  • 29,399
  • 7
  • 59
  • 71
0

Yes.

You can use <body onload='onLoad(); fan();'> as Utkanos suggests.

If you use jQuery, you can also stick a script in the head containing:

$(function(){
   ...
});

The jQuery function actually fires earlier, as is explained here.

Nick
  • 5,995
  • 12
  • 54
  • 78