Created
May 22, 2013 13:38
-
-
Save thomasgriffin/5627584 to your computer and use it in GitHub Desktop.
This is my gist for loading jQuery into your 3rd party app - because jQuery makes your life and mine much easier. This is meant to be wrapped up in some type of JS object, so you would call it like this: var my_app = new MyApp(); my_app.loadjQuery();
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Checks to see if jQuery is loaded and if it is a high enough version; if not, jQuery is loaded. | |
this.loadjQuery = function(){ | |
// Store localized copy of our main object instance. | |
var self = this; | |
// If jQuery is not present or not the correct version, load it asynchronously and fire the rest of the app once jQuery has loaded. | |
if ( window.jQuery === undefined || window.jQuery.fn.jquery !== '1.9.1' ) { | |
var om = document.createElement('script'); | |
om.src = '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'; | |
om.onload = om.onreadystatechange = function(){ | |
var s = this.readyState; | |
if (s) if (s != 'complete') if (s != 'loaded') return; | |
try { | |
self.loadjQueryHandler(); | |
} catch(e){} | |
}; | |
// Attempt to append it to the <head>, otherwise append to the document. | |
(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(om); | |
} else { | |
// The version of jQuery loaded into the window is what we want to use, so we can just script the handler and output content. | |
self.jQuery = window.jQuery; | |
self.loadApp(); | |
} | |
}, | |
// Stores a localized copy of jQuery and removes any potential conflicts with other jQuery libraries. | |
this.loadjQueryHandler = function(){ | |
// This restores the globally scoped jQuery to its original state. | |
this.jQuery = window.jQuery.noConflict(true); | |
this.loadApp(); | |
}, | |
// Callback to load the content of our app into the page. | |
this.loadApp = function(){ | |
// Store localized copy of our main object instance. | |
var self = this; | |
// Woot, we now have jQuery! | |
this.jQuery(document).ready(function($){ | |
console.log($); | |
}); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment