Mohamed Emad Hegab
Mohamed Emad Hegab

Reputation: 2675

Javascript: Load External JS file inside JS File

I'm trying to load an external Javascript file from a website to a local js file.. i've tried this

(function($) {  

  $("head").append('<script type="text/javascript" src="http://URL_TO_SITE/jquery.acornmediaplayer.js"></script>');

  setTimeout(function() {
    $('.jvideo').acornMediaPlayer();     

  }, 2000);

})(jQuery);

but it gives me

[object Object] has no method acornMediaPlayer

i've also tried $.getScript but it gives me the same result

Upvotes: 0

Views: 117

Answers (2)

Mohamed Emad Hegab
Mohamed Emad Hegab

Reputation: 2675

ok i got the problem.. it suppose to be putted inside $(window).load like this

$(window).load(function(){
    (function($) {  

  $("head").append('<script type="text/javascript" src="http://URL_TO_SITE/jquery.acornmediaplayer.js"></script>');

  setTimeout(function() {
    $('.jvideo').acornMediaPlayer();     

  }, 2000);

})(jQuery);
});

voila

Upvotes: 0

epascarello
epascarello

Reputation: 207537

My guess is you did not use getScript correctly.

jQuery.getScript( url, success);

There is a success callback when it is done. Add the function call in there and do not use the timeout.

$.getScript("http://URL_TO_SITE/jquery.acornmediaplayer.js", function(){
    $('.jvideo').acornMediaPlayer();
});

Upvotes: 4

Related Questions