-
-
Save hyyan/115508b16a5671b2029676c45a6019e0 to your computer and use it in GitHub Desktop.
Document title & History API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var button = document.createElement('button'); | |
button.textContent = 'Push'; | |
document.body.appendChild(button); | |
var x, title; | |
if (!history.state) { | |
x = 0; | |
title = document.title; | |
// Without this we can loose the initial page's title on browser navigation | |
history.replaceState({x: x, title: title}, title, location.href); | |
} else { | |
x = history.state.x; | |
} | |
button.addEventListener('click', function() { | |
x++; | |
title = 'Page ' + x; | |
history.pushState({x: x, title: title}, title, 'page-' + x); | |
// Without this we'll loose titles on push state | |
document.title = title; | |
}); | |
window.addEventListener('popstate', function(e) { | |
if (e.state) { | |
// Without this - we'll loose titles on browser navigation | |
document.title = e.state.title; | |
x = e.state.x; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment