views:

59

answers:

2

Let's say I want to give my partners a simple <script src> tag to include some dynamic content on their site, loading from my site.

Is there any way I can use some javascript framework in my .js file so I can do some more fancy things? jQuery? I assume this is a problem as it might conflict the site's own javascript code? What if they already included jQuery on their page? Or some other conflicting library?

+2  A: 

You can use jQuery's noConflict mode:

<script>
    myOwnJQuery = jQuery.noConflict(true);
</script>

You can find more details in an excellent answer to a similar question.

mtyaka
Thank you. He also wrote "It's important that the jQuery used by the original developer is loaded last". I obviously have no control about which scripts and where he runs them.
nute
Actually, I believe that part of the linked answer is wrong. As long as you call jQuery.noConflict(true) immediately after including your version of jQuery, you should be fine. The best place to do that would be at the very end of your version of jquery.js file.
mtyaka
A: 

I suggest using the LazyLoad library.

Then modify the original LazyLoad js file and at the end append the appropriate LazyLoad.XXX calls which then loads any other .js you are interested in. This is then the file you hand to your partners to include.

e.g. if you then want to load jQuery just load it via LazyLoad and use the noConflict() function.

For more details about that. Check these and similar references

Using jQuery with Other Libraries

How do I run different versions of jQuery on the same page?

jitter