Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  156] [ 1]  / answers: 1 / hits: 18597  / 15 Years ago, sun, january 31, 2010, 12:00:00

I'm developing a javascript widget that depends on jQuery. The widget may or may not be loaded onto a page that already has jQuery loaded. There are many problems that come up in this case...




  1. If the web page does not have jQuery, I must load my own jQuery. There seems to be a delicate timing issue when doing this, however. For example, if my widget loads and executes before jQuery is finished loading and executing, I get a jQuery is not defined error.


  2. If the web page does have jQuery, I can usually work with it. If the jQuery version is old, however, I would like to load my own. If I do load my own, however, I need to do it in such a way as to not stomp on their $ variable. If I set jQuery.noConflict() and any of their scripts depend on $, then I have just broken their page.


  3. If the web page uses another javascript library (e.g. prototype), I needed to be sensitive of prototype's $ variable also.




Because of all of the above, it is seeming easier to not depend on jQuery. But before I go down that road, which will involve mostly rewriting my widget code, I wanted to ask for advice first.



The basic skeleton of my code, including the timing bug and sometimes $ bugs, follows:



<script type=text/javascript charset=utf-8>
// <![CDATA
if (typeof jQuery === 'undefined') {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '{{ URL }}/jquery.js';
head.appendChild(script);
}
// ]]>
</script>
<script type=text/javascript src={{ URL }}/widget.js></script>


My widget has the following structure:



(function($) {
var mywidget = {
init: function() {
...
}
};
$(document).ready(function() {
mywidget.init();
});
})(jQuery);


If there are any pointers or resources for achieving a widget that can work in all the mentioned environments, they would be greatly appreciated.


More From » jquery

 Answers
46

After reviewing some answers and pointers, and finding some helpful jQuery hackers, I ended up with something like the following:



(function(window, document, version, callback) {
var j, d;
var loaded = false;
if (!(j = window.jQuery) || version > j.fn.jquery || callback(j, loaded)) {
var script = document.createElement(script);
script.type = text/javascript;
script.src = /media/jquery.js;
script.onload = script.onreadystatechange = function() {
if (!loaded && (!(d = this.readyState) || d == loaded || d == complete)) {
callback((j = window.jQuery).noConflict(1), loaded = true);
j(script).remove();
}
};
(document.getElementsByTagName(head)[0] || document.documentElement).appendChild(script);
}
})(window, document, 1.3, function($, jquery_loaded) {
// Widget code here
});


This will load jQuery if it's not already loaded and encapsulates it in the callback so it doesn't conflict with a pre-existing jQuery on the page. It also checks that a minimum version is available or else loads a known version -- in this case, v1.3. It sends a boolean value to the callback (my widget) on whether or not jQuery was loaded in case there are any triggers needed to be made. And only after jQuery is loaded does it call my widget, passing jQuery into it.


[#97705] Thursday, January 28, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katianatasham

Total Points: 293
Total Questions: 110
Total Answers: 103

Location: Saint Helena
Member since Mon, Jun 28, 2021
3 Years ago
katianatasham questions
Tue, Jul 20, 21, 00:00, 3 Years ago
Thu, Mar 18, 21, 00:00, 3 Years ago
Wed, Nov 25, 20, 00:00, 4 Years ago
Wed, Jun 24, 20, 00:00, 4 Years ago
Fri, May 15, 20, 00:00, 4 Years ago
;