tags:

views:

258

answers:

2
+2  Q: 

jQuery in widget

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.

+3  A: 

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
you beaut.. this is exactly what i was looking for
russau
Bare in mind that $ is not just used by jquery.
micmcg
skip the bad setTimeout after appending the script, and go for s.onload = s.onreadystatechange = function(){...and check for the 'readyState'.
vsync
A: 

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