summaryrefslogtreecommitdiff
path: root/js/loadPage.ts
blob: 72bb124c5629255b0fbf7b2db6838e9152b71d4e (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(page: string) {
	console.log("loading page `" + page + "`")

	let url = `/html/${page}.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 content    = document.getElementById("content")!;

	let new_title      = dom.getElementById("title")!;
	let new_content    = dom.getElementById("content")!;

	title.replaceWith(new_title);
	body.setAttribute("data-page", page);
	content.replaceWith(new_content);

	initImages();
}