-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
83 lines (65 loc) · 2.7 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Polyfill Example</title>
</head>
<body>
<script src="polyfill-rollup.js"></script>
<script>
window.addEventListener("load", loaded)
function loaded() {
console.log("Initial History State", history.state);
console.log(window.navigation)
const urlSpan = document.getElementById("url");
const app = document.getElementById("app");
window.navigation.addEventListener(
"navigate",
event => {
console.log("Navigate event", event)
const url = new URL(event.destination.url);
console.log(event.destination.url);
console.log("Submitted Name:", event.formData?.get("name"));
console.log("Previous State:", window.navigation.currentEntry.getState());
console.log("State:", event.destination.getState());
console.log("Same Document:", event.destination.sameDocument);
urlSpan.innerText = url.pathname;
app.innerHTML = "";
if (url.pathname === "/some/other") {
console.log("Preventing default");
event.preventDefault();
try {
console.log("Navigated");
window.navigation.navigate("/", {
state: "returned"
});
} catch (error) {
console.error(error);
}
} else if (url.pathname === "/some/page") {
event.intercept(() => {
app.innerHTML = `
<form action="/" method="post">
<input type="text" name="name" value="" placeholder="Name" />
<button type="submit">Submit</button>
</form>
`;
})
} else {
event.intercept()
}
}
);
if (window.navigation.currentEntry) {
urlSpan.innerText = new URL(window.navigation.currentEntry.url).pathname;
}
console.log(window.navigation.currentEntry.url)
}
</script>
<p>Current Url <span id="url"></span></p>
<a href="/">Home Link</a>
<a href="/some/page">Some Page Link</a>
<a href="/some/other">Some Other Page Link</a>
<div id="app"></div>
</body>
</html>