summaryrefslogtreecommitdiff
path: root/js/loadPage.ts
blob: ebf1f62ef05f42328d9f64e3a5b59dc49ec28c86 (plain) (blame)
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
/// <reference path="initImages.ts" />

async function loadPage(name: string) {
	console.log("loading page `" + name + "`")

	let url = `/html/${name}.html`;
	window.history.pushState("", "", url);

	let response = await fetch(url);

	if (!response.ok) {
		throw new Error(`unable to load page: \"${response.status}\"`);
	}

	let markup = await response.text();

	let parser = new DOMParser();
	let dom = parser.parseFromString(markup, "text/html");

	let title = document.getElementById("title")!;
	let body  = document.getElementById("body")!;
	let page  = document.getElementById("page")!;

	let new_title = dom.getElementById("title")!;
	let new_page  = dom.getElementById("page")!;

	title.replaceWith(new_title);
	body.setAttribute("data-page", name);
	page.replaceWith(new_page);

	initImages();
}