Skip to content

Instantly share code, notes, and snippets.

@basememara
Created February 27, 2013 15:15
Show Gist options
  • Save basememara/5048648 to your computer and use it in GitHub Desktop.
Save basememara/5048648 to your computer and use it in GitHub Desktop.
RequireJS: Avoid double jQuery load and version conflict in main.js
;(function () {
var paths = { ... };
//HANDLE JQUERY IF LOADED ALREADY TO AVOID OVERWRITING EXISTING JQUERY PROPERTIES AND PLUGINS
//CHECK FOR OLD VERSIONS OF JQUERY
var oldjQuery = !!(window.jQuery && !!window.jQuery.fn.jquery.match(/^1\.[0-4]/));
//LOAD JQUERY IF NOT AVAILABLE OR BELOW MIN
if (!window.jQuery || oldjQuery) {
paths.jquery = [
'//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min',
//If the CDN location fails, load from this location
'libs/jquery/jquery.min'
];
} else {
//REGISTER THE CURRENT JQUERY
define('jquery', [], function () { return window.jQuery; });
}
//CONFIGURE REQUIRE JS
require.config({
...
paths: paths,
...
});
//START REQUIRE JS
require([
'jquery',
'app'
], function ($, App) {
//HANDLE MULTIPLE JQUERY VERSIONS IF NECESSARY
if (oldjQuery) $.noConflict(true);
//INITIALIZE APP
App.init();
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment