- I have one short script that client's add to their website to load the application that we offer
- This code asynchronously loads the JavaScript that launches the application, onto the client's page (a pop up)
- The code to launch the application (launch code :-) requires jQuery
- Some client's use jQuery on their site all ready... some don't...
- What I really really want to achieve is, the code that gets loaded onto the page checks the document for the presence of JQuery, and if it isn't already there, loads it onto the page, so the launch code can make use of it
Now I have spent a while getting this JavaScript together and I have got to the stage that jQuery is getting added to the page when needed but any subsequent script can't seem to use it unless I have added an alert!! like alert('Hello World');
My thought is that the alert function manages to get the DOM to 'reload' in some way which updates the jQuery link in the DOM... my understanding definitely falls down at this point...
Any help will be much appreciated, thanks in advance
Here is what I have, the following is the part of the 'launch code' that appends jQuery onto the client's page, this is externally linked to by the loading script mentioned in point 1.
if (typeof jQuery == 'undefined') {
//alert('No jQuery! Will attempt to load it now');
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(script);
tryReady = function(time_elapsed) {
if (typeof jQuery == "undefined") { // if jQuery isn't loaded yet...
if (time_elapsed <= 100000) { // and we havn't given up trying...
setTimeout("tryReady(" + (time_elapsed + 200) + ")", 200); // set a timer to check again in 200 ms.
} else {
alert('hello'); // If this isn't here, the subsequent scripts fail to access jQuery
}
} else {
//alert ("jQuery wasn't but now is loaded")
}
}
tryReady();
}
// Functions making use of jQuery go here...