-
-
Notifications
You must be signed in to change notification settings - Fork 677
/
import.js
37 lines (34 loc) · 1002 Bytes
/
import.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
if( "forEach" in NodeList.prototype ) {
function importContent(el) {
if (!("fetch" in window) || "connection" in navigator && navigator.connection.saveData === true) {
return;
}
let targetUrl = el.getAttribute("data-import");
fetch(targetUrl).then(function (response) {
return response.text();
}).then(function (text) {
el.innerHTML = text;
el.removeAttribute("data-import");
el.setAttribute("data-import-complete", "");
}).catch((err) => {
console.log('Import failed', err);
});
}
if(typeof IntersectionObserver !== "undefined") {
var observer = new IntersectionObserver(function(changes) {
changes.forEach(function(change) {
if(change.isIntersecting) {
importContent(change.target);
observer.unobserve(change.target);
}
});
});
}
document.querySelectorAll("[data-import]").forEach(function(element) {
if(typeof IntersectionObserver !== "undefined") {
observer.observe(element);
} else {
importContent(element);
}
});
}