Skip to content

Instantly share code, notes, and snippets.

Created November 19, 2017 20:32
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
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 = "";
if (js.readyState)
js.onreadystatechange = function () {
if (this.readyState == 'complete' || this.readyState == 'loaded') {
js.onload = jQueryLoadHandler;
(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(js);
} else {
function jQueryLoadHandler() {
Copy link

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

Copy link

wuori commented Apr 9, 2019

Copy link

@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