So I have trying to build a embeddable widget that will play nicely w/others when it is loaded on an external website. Since I have never had to do this before I was following a great tutorial by Alex Marandon on how to build a web widget. I am able to load jquery (1.7.1), however, whenever I attempt to load jquery-ui (1.8.16) - from w/in the JS - I get a JS error that says "a is undefined". The following fiddle reproduces what I am seeing:
http://jsfiddle.net/malonso/qfBLx/
Now if I combine the jquery and jqueryui into a single file and put it on my server it works fine but that is less than optimal for a variety of reasons and I would like to avoid that if possible. The other strange thing is that if I attempt to load any other JS file in place of jqueryui, the file loads just fine; clearly I am missing something there.
Thanks in advance.
Update:
The fiddle contains all the relevant code but I will include the javascript portion below. The code waits until the browser has loaded jquery and then it makes a request to load jqueryui. I actually use jquery to print debug statements before and after the request to load jqueryui, so jquery is definitely "available". Just for giggles I tried delaying the loading of jqueryui until 3 seconds after the browser determine jquery is loaded and I get the same issue.
(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.7.1') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src","http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else {
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main();
}
/******** Our main function ********/
function main() {
jQuery(document).ready(function($) {
loadJS();
});
}
function loadJS(){
jQuery('#debug').append("About to load jquery-ui<br>");
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src","http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js");
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
jQuery('#debug').append("Request made to load jquery-ui<br>");
}
})(); // We call our anonymous function immediately