-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathinclude-html.js
More file actions
26 lines (22 loc) · 814 Bytes
/
include-html.js
File metadata and controls
26 lines (22 loc) · 814 Bytes
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
document.addEventListener("DOMContentLoaded", (e) => {
const includeHTML = (el, url) => {
const xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", (e) => {
if (xhr.readyState !== 4) return;
if (xhr.status >= 200 && xhr.status < 300) {
el.outerHTML = xhr.responseText;
} else {
let message =
xhr.statusText ||
"Error loading the file, verify that you are making the request by http or https";
el.outerHTML = `<div><p>Error ${xhr.status}: ${message}</p></div>`;
}
});
xhr.open("GET", url);
xhr.setRequestHeader("Content-type", "text/html; charset=utf-8");
xhr.send();
};
document
.querySelectorAll("[data-include]")
.forEach((el) => includeHTML(el, el.getAttribute("data-include")));
});