Page 1 of 1

running script with onload

Posted: Sat Jul 30, 2011 1:05 am
by amc
I have a script that I load in the external javascript attribute of a screen. It has one function named setVisible.

I would like to call this script as soon as the screen is loaded so I tried the onload event for the screen, but that seems to be triggered before the external javascript is fetched which means that I get an onload error: setVisible is not defined

Is it possible to call a script at onready time (after the external script is loaded) rather than at onload time (before the external script is loaded) ?

I know it will work at onload time if I have it saved load it into custom/js directory, but that means the script is sent to every program that get's run, even though it is only required/run in one program.

thanks & regards

Tony C

Re: running script with onload

Posted: Wed Aug 03, 2011 2:06 pm
by Brian
In general, your javascript files are loaded asynchronously. There is no guarantee that the file will be loaded in time for onload.

One of the easiest ways to get around this is to actually include the the js file on the screen before this one if possible. The browser will keep the function available after you load the screen that needs the function. If this is not feasible, I can try to assist you in setting up something more complicated.

Re: running script with onload

Posted: Thu Aug 04, 2011 8:44 pm
by amc
Thanks Brian,

I changed to design to have a screen preceding the filter screen & loaded my js there. This solves the problem.

Re: running script with onload

Posted: Fri Nov 02, 2012 12:21 pm
by Bryan641
It'd be great to have an "onready" event or something that would get fired AFTER external scripts have been loaded.

What I've done is create onload event handlers like this:

Code: Select all

function tryInit() { 
     if (typeof(InitFunction)!='undefined') { 
          InitFunction();   // defined in an external javascript file
     } else { 
         setTimeout( 'tryInit()', 300) 
     } 
}  
tryInit();
It's ***, but works, especially since I can't format it nicely in the onload even box.

--Bryan