Skip to content

Instantly share code, notes, and snippets.

@sebastianrothbucher
Created September 3, 2019 19:07
Show Gist options
  • Save sebastianrothbucher/7a373bed0c924a6654402006f84bc5f6 to your computer and use it in GitHub Desktop.
Save sebastianrothbucher/7a373bed0c924a6654402006f84bc5f6 to your computer and use it in GitHub Desktop.
mini pushstate demo (npm run start)
<html>
<body>
<script>
function doMagic() {
event.preventDefault();
fetch('page2.json').then(res => res.json()).then(res => {
Object.keys(res).forEach(k => document.getElementById(k).textContent=res[k]);
history.pushState({}, null, "page2.html");
});
}
</script>
<h1 id="hOne">Headline index</h1>
<p id="para">Content index <a href="page2.html" onclick="doMagic()">go to page2</a></p>
</body>
</html>
{
"name": "pushstate",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {
"http-server": "0.11.1"
},
"scripts": {
"prestart": "npm install",
"start": "http-server ."
}
}
<html>
<body>
<h1 id="hOne">Headline page2</h1>
<p id="para">Content page2 (via Server)</p>
</body>
</html>
{
"hOne": "Headline page2",
"para": "Content page2 (via JSON)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment