Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
jQuery async loader to page
function ensureJquery(readyCallback) {
if (window.jQuery === undefined || parseFloat(window.jQuery.fn.jquery) < 1.9) {
var js = document.createElement('script');
js.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js";
if (js.readyState)
js.onreadystatechange = function () {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
jQueryLoadHandler();
}
};
else
js.onload = jQueryLoadHandler;
(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(js);
} else {
readyCallback(window.jQuery);
}
function jQueryLoadHandler() {
readyCallback(window.jQuery.noConflict(true));
}
}
@khaliullin

This comment has been minimized.

Copy link

commented Mar 29, 2019

It's not very clear how to use it for me. Could you post any example, please?

@wuori

This comment has been minimized.

Copy link

commented Apr 9, 2019

@jenyayel

This comment has been minimized.

Copy link
Owner Author

commented Apr 13, 2019

@khaliullin, just noticed your comment. You call the method ensureJquery and provide callback, which will be called after:

  • In case there is already loaded jQuery on page with version 1.9 or higher (you can change the version number)
  • In case there is no jQuery on page at all, or it is lower than 1.9, then the method will load and inject it into page without conflicting with the version presented on the page

The callback that you specify will get an argument, which will be jQuery object in version 1.9 or higher without changing window global object jQuery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.