javascript - .getScript() callback for multiple usage -


if have 2 js files a.js , b.js

a.js

$.getscript('some.js',function(){    //call function in some.js }); 

in file b.js, call function some.js, how know some.js loaded ?

can like.. (in b.js)

somejsisloaded.then(function(){    //call function in some.js }); 

.then() .promise() ?

$.getscript() returns jqxhr promise, can assigned later use.

you write ...

var somejspromise = $.getscript('some.js', function() {     //call function in some.js }); 

... it's better not use global namespace, use jquery namespace :

$.somejspromise = $.getscript('some.js', function() {     //call function in some.js }); 

in case script 'some.js' doesn't load, should include error handling. @ least, log error :

$.somejspromise = $.getscript('some.js', function() {     //call function in some.js }).then(null, function(error) {     console.log('getscript(some.js)', error); }); 

thereafter, safe way run script delivered in "some.js", use assigned promise's .then() method :

$.somejspromise.then(function() {     //call function in some.js }, function() {     //handle case some.js fails load. }); 

if some.js has loaded, callback run immediately.

if some.js not yet loaded, callback queued, , run if/when some.js loads.

if some.js has failed (or fail), error handler run.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -