Skip to content

Instantly share code, notes, and snippets.

@mloberg
Created June 29, 2011 20:05
Show Gist options
  • Save mloberg/1054803 to your computer and use it in GitHub Desktop.
Save mloberg/1054803 to your computer and use it in GitHub Desktop.
Simple jQuery Plugin Loader
(function($){
$.load = function(options){
// check for something to load
if(options.src === undefined) return;
// load dependencies
if(options.deps !== undefined){
$.each(options.deps, function(key, value){
var type = value.split(".")[value.split(".").length - 1];
if(type === "js"){
$.getScript(value);
}else if(type === "css"){
var css = document.createElement("link");
css.rel = "stylesheet";
css.href = value;
// append the new element
document.getElementsByTagName("head")[0].appendChild(css);
}
});
}
// load the script
$.getScript(options.src, function(){
// run the callback function if there is one
if(options.callback !== undefined){
options.callback();
}
});
};
})(jQuery);
$(document).ready(function(){
$.load({
src: "js/jquery.plugin.js", // the location of the script relative to the page (required)
deps: ["css/plugin.css", "js/jquery.dependecy.js"], // dependencies of the script (optional),
callback: function(){ // will run after the plugin has loaded (optional)
// do stuff with your plugin here
}
});
});
@fedek6
Copy link

fedek6 commented Sep 23, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment