is concatenating the only way to 'import' one JS lib from another?

Posted by Nikita on Stack Overflow See other posts from Stack Overflow or by Nikita
Published on 2010-04-21T05:01:44Z Indexed on 2010/04/21 5:03 UTC
Read the original article Hit count: 234

Filed under:
|
|

Disclaimer: JS novice

I have a JS widget that depends on JQuery. The widget's going to be embedded in a 3rd party site but I figure out how to avoid declaring dependency on jquery on the widget-hosting page:

3rd party's page:

<head>

<script
  type="text/javascript"
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>

<script
  type="text/javascript"
  src="http://mydomain/mywidget.js"></script>

</head>

mywidget.js

jQuery(document).ready(function() {
     //do stuff
});

I'd rather not include jquery.js in the 3d party page but express the dependency inside mywidget.js (so i can change this dependency or add/remove others w/o having to update the widget-hosting page)

I tried adding:

var script = document.createElement('script');
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);

to the top of mywidget.js but that didn't work - jquery.js did load on page load but "jQuery" was not recognized.

What did work was concatenating jquery.js and mywidget.js into a single .js file. But that seems kind of lame - is there no equivalent to?

import com.jquery.*;

thanks!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery