Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  180] [ 2]  / answers: 1 / hits: 25737  / 15 Years ago, wed, july 15, 2009, 12:00:00

I'm using the following bit of script to load another one:



$.getScript(CAGScript.js, function () {
try {
CAGinit();
} catch(err) {
console.log(err);
}
});


The idea is that $.getScript loads the script, then executes the callback when it's done. CAGInit() is a function that lives in CAGScript.js.



The problem is that roughly half the time, CAGInit() doesn't fire (in any browser). Logging to the Firebug console reports that it's not defined. The rest of the time it works perfectly.



Does anyone have any ideas what I'm doing wrong?



Thanks.


More From » jquery

 Answers
57

In order to make sure CAGScript.js is LOADED and EXECUTED before calling CAGinit function, the surest way is to have the function call inside CAGScript.js.



CAGScript.js:





...
/*
your code
*/
function CAGinit(){
...
}
...
/* last line */
CAGinit();



and then, in your main file just call getScript():



 

$.getScript(CAGScript.js);


[#99122] Friday, July 10, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
debra

Total Points: 583
Total Questions: 111
Total Answers: 111

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
;