Skip to content

Instantly share code, notes, and snippets.

@t-okushima
Created July 13, 2017 15:49
Show Gist options
  • Save t-okushima/003c0e3d6a98cf11e287c59dae09a6f3 to your computer and use it in GitHub Desktop.
Save t-okushima/003c0e3d6a98cf11e287c59dae09a6f3 to your computer and use it in GitHub Desktop.
History APIで履歴をいじる ref: http://qiita.com/t-okushima/items/9f01d8a033e16557e62e
window.history.back();
window.history.forward();
window.history.go(1)
window.history.pushState({name : 't-okushima'}, null, '/name')
window.history.replaceState({name : 'aaaa'}, null, '/name')
window.addEventListener('popstate', function(event) {
// 任意の処理
var state = event.state;
alert(state.name):
});
window.history.state
<!DOCTYPE html>
<html>
<head>
<title>History API Sample</title>
</head>
<body>
<label for='name'>Name</label>
<input type='text' id='name' value='' />
<input type='button' id='pushButton' value='pushState' />
<hr />
<input type='button' id='replaceButton' value='replaceState' />
<script>
document.getElementById('pushButton').addEventListener('click', function() {
var name = document.getElementById('name').value;
window.history.pushState({name : name}, null, './history-api.html');
});
document.getElementById('replaceButton').addEventListener('click', function() {
var name = document.getElementById('name').value;
window.history.replaceState({name : name}, null, './history-api.html');
});
window.addEventListener('popstate', function(e) {
var state = e.state;
if (state) {
document.getElementById('name').value = state.name;
alert(state.name);
} else {
document.getElementById('name').value = '';
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment