User:Sigilbaram/Ajax load

/************* * Ajax Load * *************/

var ajaxCache = new Object;

window.ajaxLoad = function(el,source,fragment) { if(source) { $(el).html('  '); if (fragment) { var url = wgScript + '?title=' + source.replace(/ /g,'_').replace(/\?/g,'%3F') + '&action=render ' + fragment; } else { var url = wgScript + '?title=' + source.replace(/ /g,'_').replace(/\?/g,'%3F') + '&action=render'; }

if (ajaxCache[url]) { $(el).html(ajaxCache[url]); } else { $(el).load(url,function( response, status, xhr ){               if( status == "error" ) {                    $(this).html('Ajax failed to load: ' + xhr.status + " " + xhr.statusText);                } else {                    ajaxCache[url] = response;                }            }); }   } else { console.log("Can't load ajax without a source URL."); return; } }

/** * A simple example where elements with the ajax-load class are loaded using * their data-ajax-source and data-ajax-fragment attributes for source and fragement. */ $('.ajax-load').each(function {   ajaxLoad( this, $(this).attr('data-ajax-source'), $(this).attr('data-ajax-fragment') ); });