Skip to content

Commit

Permalink
Add history api example (#236)
Browse files Browse the repository at this point in the history
* Initial version

* History API example

* Set alt

* Not cats

* Unused param

* Comments

* Show page in URL bar

* Add history-api example to README, also Prettier

* Better names

* Apply suggestions from code review

Co-authored-by: Jean-Yves Perrier <[email protected]>

* Update title for home page too

---------

Co-authored-by: Jean-Yves Perrier <[email protected]>
  • Loading branch information
wbamberg and teoli2003 authored Nov 17, 2023
1 parent 0265129 commit 4e65005
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 35 deletions.
73 changes: 38 additions & 35 deletions README.md

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions history-api/creatures/eagle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"description": "Bald eagles are not actually bald.",
"image": {
"src": "images/eagle.jpg",
"alt": "A bald eagle"
},
"name": "Eagle"
}
8 changes: 8 additions & 0 deletions history-api/creatures/turtle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"description": "Painted turtles have anti-freeze in their blood.",
"image": {
"src": "images/turtle.jpg",
"alt": "A painted turtle"
},
"name": "Turtle"
}
8 changes: 8 additions & 0 deletions history-api/creatures/vulture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"description": "Turkey vultures have a keen sense of smell.",
"image": {
"src": "images/vulture.jpg",
"alt": "A turkey vulture"
},
"name": "Vulture"
}
Binary file added history-api/images/eagle.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added history-api/images/puffin.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added history-api/images/turtle.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added history-api/images/vulture.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions history-api/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Creatures: Home</title>
<script src="script.js" defer></script>
</head>

<body>
<ul class="nav">
<li><a href="/">Home</a></li>
<li><a href="eagle.html" data-creature="eagle">Eagle</a></li>
<li><a href="vulture.html" data-creature="vulture">Vulture</a></li>
<li><a href="turtle.html" data-creature="turtle">Turtle</a></li>
</ul>

<p id="description">Let's look at some creatures.</p>

<img id="photo" src="images/puffin.jpg" alt="A puffin" />
</body>
</html>
53 changes: 53 additions & 0 deletions history-api/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Update the page with the new content
function displayContent(content) {
document.title = `Creatures: ${content.name}`;

const description = document.querySelector("#description");
description.textContent = content.description;

const photo = document.querySelector("#photo");
photo.setAttribute("src", content.image.src);
photo.setAttribute("alt", content.image.alt);
}

// When a link is clicked: update the page for that creature.
// From a user perspective, this will look like the loading of a new page.

document.addEventListener("click", async (event) => {
const creature = event.target.getAttribute("data-creature");
if (creature) {
event.preventDefault();
try {
const response = await fetch(`creatures/${creature}.json`);
const json = await response.json();
displayContent(json);
// Add a new entry to the history.
// This simulates loading a new page.
history.pushState(json, "", creature);
} catch (err) {
console.error(err);
}
}
});

// Handle forward/back buttons
window.addEventListener("popstate", (event) => {
// If a state has been provided, we have a "simulated" page
// and we update the current page.
if (event.state) {
// Simulate the loading of the previous page
displayContent(event.state);
}
});

// Create state on page load and replace the current history with it
const image = document.querySelector("#photo");
const initialState = {
description: document.querySelector("#description").textContent,
image: {
src: image.getAttribute("src"),
alt: image.getAttribute("alt"),
},
name: "Home",
};
history.replaceState(initialState, "", document.location.href);

0 comments on commit 4e65005

Please sign in to comment.