Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a Javascript file within another Javascript file and execute file 2 before file 1?

I have written a jQuery plugin, say jquery.plugin.js. I want to use this plugin on a large number of sites.

What I want to do is to write a piece of js code at the top of jquery.plugin.js which will load the jquery.main.js and execute it so that $ is available to be used in jquery.plugin.js.

Thanks.


2 Answers

What you probably want to do is create a new tag in js

script = document.createElement('script');
script.src = 'URL-TO-JQUERY';

And then append that element before the first tag.

document.body.insertBefore(document.getElementsByTagName('script')[0], script);

This will most probably work as the script is inserted before any other js. (except in the head)

Yes, just a interval that checks if jQuery exists:

interval = setInterval(check, 100);
function check(){
  if($.version){
    // Exec script
  }
}
like image 70
Joseph Adams Avatar answered May 04 '26 19:05

Joseph Adams


This page may be useful for you

http://code.google.com/p/jquery-ajaxq/

with this script you can use files sequentially

like image 22
mjimcua Avatar answered May 04 '26 20:05

mjimcua



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!