-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscript.js
More file actions
28 lines (24 loc) · 979 Bytes
/
script.js
File metadata and controls
28 lines (24 loc) · 979 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
27
28
// add comments to descript what this script does
document.addEventListener("DOMContentLoaded", () => {
// pages as an array
const pages = [
{ name: "Home", file: "default.htm", id: "home" },
{ name: "About", file: "about.htm", id: "about" },
{ name: "Examples", file: "examples.htm", id: "examples" }
];
// navigation section
const nav = document.createElement("nav");
nav.innerHTML = pages
.map(page => `<a href="${page.file}" data-page="${page.id}">${page.name}</a>`)
.join("");
// add navigation to html file
document.body.insertBefore(nav, document.body.firstChild);
// active link should look different - determine active page and add "active" class to the link
const currentPage = window.location.pathname.split("/").pop().split(".")[0];
const links = nav.querySelectorAll("a");
links.forEach(link => {
if (link.dataset.page === currentPage) {
link.classList.add("active");
}
});
});