Skip to content

Instantly share code, notes, and snippets.

@larryprice
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larryprice/b06dd00c3e7f63ae7954 to your computer and use it in GitHub Desktop.
Save larryprice/b06dd00c3e7f63ae7954 to your computer and use it in GitHub Desktop.
<div id="content" style="margin-bottom: 1.5em;">Welcome</div>
<button id="continue" onclick="nextPage()">
Continue
</button>
<script type="text/javascript" async>
function nextPage() {
var content = document.getElementById("content"),
nextPage = 1;
if (content.innerHTML === "Welcome") {
updatePageContent(nextPage);
} else {
nextPage = parseInt(content.innerHTML.slice(5))+1;
updatePageContent(nextPage);
}
history.pushState(nextPage, null, "?page=" + nextPage);
}
function updatePageContent(pageNumber) {
var content = document.getElementById("content");
if (pageNumber === 0) {
content.innerHTML = "Welcome";
} else {
content.innerHTML = "Page " + pageNumber;
}
}
window.onpopstate = function(event) {
updatePageContent(event.state);
}
window.onload = function() {
var currentPage = parseInt(window.location.search.slice(6)) || 0;
updatePageContent(currentPage);
history.replaceState(currentPage, null, window.location.search);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment