Skip to content

Instantly share code, notes, and snippets.

@dtomasi dtomasi/ContentLoader.js
Last active Aug 31, 2018

Embed
What would you like to do?
ContentLoader loads HTML from given UrlObject via Ajax. It returns all data via complete-Function if all ist recieved. onlyContainer-Option allows to fetch a specified Element from Url eg. returns only specified element.
/**
* JQuery Plugin ContentLoader
* @copyright tomasiMEDIA 2013
* @author Dominik Tomasi
* @date 09.10.13
*/
(function($,window){
$.ContentLoader.defaultOptions = {
urlObject: {},
onlyContainer: null,
complete: function(){}
};
$.ContentLoader = function(el, options){
var base = this;
base.$el = $(el);
base.el = el;
base.$el.data("ContentLoader", base);
//Constructor
base.init = function(){
base.options = $.extend({},$.ContentLoader.defaultOptions, options);
base.content = {};
$.each( base.options.urlObject ,function(index,url){
$.get( url, function( data ) {
var saveData;
if( base.options.onlyContainer !== null ){
saveData = $(data).find(base.options.onlyContainer);
} else {
saveData = data;
}
base.content[index] = saveData;
if( objectLength(base.content) == objectLength(base.options.urlObject)){
console.log( "complete" );
base.options.complete(base.content);
}
});
});
};
// Run Constructor
base.init();
};
$.fn.ContentLoader = function(options){
return this.each(function(){
(new $.ContentLoader(this, options));
});
};
window.ContentLoader = function(options){
(new $.ContentLoader(this, options));
};
})(jQuery,window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.