5

I want to use jQuery in an add on JS library that can be added to random websites. These websites may or may not use jQuery already.

I have 3 questions around this actually:

  1. I will probably load jQuery dynamically from my own js script (not from a script tag in the document head). Will jquery work this way? how can I make sure it will run in time without having the standard $(document).ready(function(){} in the main document?

  2. What should I do to avoid conflicts with existing jQuery (if any) in the web site code.

  3. Is there a recommended way to add a widget that includes jQuery to random websites while providing minimal code and simplest integration.

Nir
  • 24,619
  • 25
  • 81
  • 117

2 Answers2

5

This is pretty loose and incomplete -- and really is meant to be a starting point:

if (typeof $ != 'undefined') {
    var msg = 'This page already using jQuery v' + $.fn.jquery;
} else {
    var s = document.createElement('script');
    s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js');
    document.getElementsByTagName('head')[0].appendChild(s);
    var msg = 'This page is now jQuerified';
}

then wait via a brief setTimeout() before running a ready() function

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
  • you beaut.. this is exactly what i was looking for – russau Jun 17 '09 at 21:49
  • 1
    skip the bad setTimeout after appending the script, and go for s.onload = s.onreadystatechange = function(){... and check for the 'readyState'. – vsync Apr 27 '10 at 11:57
0

You can try the solution in the following link if it works for you. Basically, there is a plugin that creates widgets on the fly and then asynchronously requests another page url, and sets the returned content as inner html of the widget.

http://sites.google.com/site/spyderhoodcommunity/tech-stuff/jquerydashboardwidgetplugin

Kartik Sehgal
  • 143
  • 2
  • 8