Skip to content

Instantly share code, notes, and snippets.

@ddesilva
Created July 9, 2012 23:17
Show Gist options
  • Save ddesilva/3079714 to your computer and use it in GitHub Desktop.
Save ddesilva/3079714 to your computer and use it in GitHub Desktop.
Require JS Ajax Content Loader (Preloads External HTML & Images)
define(["require", "exports"], function (a, b) {
b.ajaxLoader = function (a) {
function g() {
for (var a = 0; a < n.length; a++) {
if (n[a].removeEvent) {
n[a].removeEvent("onload")
}
}
}
function f(a) {
var b = l[a];
if (b != undefined) {
if (b.noIOS && m) {
return
}
$.ajax({
url: b.url,
cache: b.cache,
dataType: b.dataType || "text",
success: function (a) {
$(b.targetContainer).html($(a).filter(b.extractHTML).html());
e(b.images)
window['_html'] = a;
}
})
}
}
function e(a) {
h = a;
var b = new Array;
for (i = 0; i < h.length; i++) b[i] = d(h[i])
}
function d(a) {
var b = new Image;
if (b.addEventListener) {
b.addEventListener("load", c, false)
} else if (b.attachEvent) {
b.attachEvent("onload", c)
}
b.src = a;
n.push(b);
return b
}
function c() {
j++;
if (j == h.length) {
j = 0;
if (k < l.length) {
l[k].success && l[k].success();
k++;
f(k);
g()
}
}
}
function b(a) {
l = a.contentToLoad;
m = a.isIOS;
f(k)
}
var h;
var j = new Number(0);
var k = new Number(0);
var l;
var m;
var n = [];
var o = {
initWithSettings: b
};
o.initWithSettings(a);
return o
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment