-
Notifications
You must be signed in to change notification settings - Fork 7
/
HTMLInclude.js
65 lines (65 loc) · 2.93 KB
/
HTMLInclude.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*! HTMLInclude v1.1.1 | MIT License | github.com/paul-browne/HTMLInclude */
!function(w, d) {
if (!w.HTMLInclude) {
w.HTMLInclude = function() {
function isInViewport(element, offset) {
return element.getBoundingClientRect().top <= (+offset + w.innerHeight);
}
function ajax(url, elements) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
elements.forEach(function(element) {
var dataReplace = element.getAttribute("data-replace");
var z = xhr.responseText;
if (dataReplace) {
dataReplace.split(",").forEach(function(el) {
var o = el.trim().split(":");
z = z.replace(new RegExp(o[0], "g"), o[1]);
});
}
element.outerHTML = z;
var scripts = new DOMParser().parseFromString(z, 'text/html').querySelectorAll("SCRIPT");
var i = 0;
var j = scripts.length;
while (i < j) {
var newScript = d.createElement("SCRIPT");
scripts[i].src ? newScript.src = scripts[i].src : newScript.innerHTML = scripts[i].innerHTML;
d.head.appendChild(newScript);
i++;
}
});
}
};
xhr.open("GET", url, true);
xhr.send();
}
function lazyLoad(element, offset) {
w.addEventListener("scroll", function scrollFunc() {
if (isInViewport(element, offset)) {
w.removeEventListener("scroll", scrollFunc);
ajax(element.getAttribute("data-include"), [element]);
}
})
}
var store = {};
var dis = d.querySelectorAll('[data-include]:not([data-in])');
var i = dis.length;
while (i--) {
var di = dis[i].getAttribute('data-include');
var laziness = dis[i].getAttribute('data-lazy');
dis[i].setAttribute("data-in", "");
if (!laziness || (laziness && isInViewport(dis[i], laziness))) {
store[di] = store[di] || [];
store[di].push(dis[i]);
} else {
lazyLoad(dis[i], laziness);
}
}
for (var key in store) {
ajax(key, store[key]);
}
}
}
w.HTMLInclude();
}(window, document)