Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: how to know when an external JS has finished?

I need to execute specific JavaScript instructions AFTER an external JavaScript finishes its own process.

(function(){
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript'; dsq.async = true;
    dsq.src = 'http://xxxxxxxx.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();

How can jQuery know when that .js has finished doing what it does?

like image 263
Andres SK Avatar asked Dec 22 '22 02:12

Andres SK


1 Answers

The $.getScript method can load script files even from external domains, and you can provide a callback function, that will be executed when if the script is loaded successfully:

$.getScript('http://xxxxxxxx.disqus.com/embed.js', function () {
  // specific instructions here...
  alert('Script loaded!');
});

Check the above example here.

like image 83
Christian C. Salvadó Avatar answered Jan 13 '23 18:01

Christian C. Salvadó