Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include jQuery from another JavaScript file

I am trying to include jQuery from a javascript file. I have tried the following, although it doesn't work.

var script = '<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>';
document.getElementsByTagName('head')[0].appendChild(script);
like image 271
Harry Avatar asked Oct 21 '25 18:10

Harry


1 Answers

</script> closes the opening <script> block, even if it's in a string. I would do it this way:

(function() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = document.location.protocol + '//code.jquery.com/jquery-1.9.1.min.js';

    document.getElementsByTagName('head')[0].appendChild(script)
})();
like image 126
Blender Avatar answered Oct 24 '25 09:10

Blender



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!