Created
June 29, 2011 20:05
-
-
Save mloberg/1054803 to your computer and use it in GitHub Desktop.
Simple jQuery Plugin Loader
This file contains 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
(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); |
This file contains 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
$(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 | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!