-
Notifications
You must be signed in to change notification settings - Fork 0
/
router-dom.html
57 lines (51 loc) · 1.8 KB
/
router-dom.html
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Minha SPA</title>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/login">Login</a></li>
<li><a href="/registro">Registro</a></li>
<li><a href="/loja">Loja</a></li>
</ul>
</nav>
</header>
<div id="conteudo"></div>
<script>
function carregarConteudo(url) {
const conteudoDiv = document.getElementById("conteudo");
if (url === "/") {
conteudoDiv.innerHTML = "<h1>Página inicial</h1>";
} else if (url === "/login") {
conteudoDiv.innerHTML = "<h1>Página de login</h1>";
} else if (url === "/registro") {
conteudoDiv.innerHTML = "<h1>Página de registro</h1>";
} else if (url === "/loja") {
conteudoDiv.innerHTML = "<h1>Página da loja</h1>";
} else {
conteudoDiv.innerHTML = "<h1>Página não encontrada</h1>";
}
}
window.addEventListener("popstate", function(event) {
const url = event.state ? event.state.url : window.location.pathname;
carregarConteudo(url);
});
document.addEventListener("click", function(event) {
if (event.target.tagName === "A" && event.target.getAttribute("href").startsWith("/")) {
event.preventDefault();
const url = event.target.getAttribute("href");
history.pushState({ url: url }, "", url);
carregarConteudo(url);
}
});
window.addEventListener("load", function() {
carregarConteudo(window.location.pathname);
});
</script>
</body>
</html>