Skip to content

Instantly share code, notes, and snippets.

@hideya
Last active May 23, 2018 01:24
Show Gist options
  • Save hideya/8f27d501c2dbe68660756a88f740b5f8 to your computer and use it in GitHub Desktop.
Save hideya/8f27d501c2dbe68660756a88f740b5f8 to your computer and use it in GitHub Desktop.
document.addEventListener("DOMContentLoaded", function() {
var storage = window.sessionStorage;
try {
if (window.location.pathname.includes('#')) {
storage.removeItem('scroll-x');
storage.removeItem('scroll-y');
return;
}
var x = storage.getItem('scroll-x');
var y = storage.getItem('scroll-y');
if (x && y) {
window.scrollTo(x, y);
}
window.addEventListener('pagehide', function() {
try {
storage.setItem('scroll-x', window.pageXOffset);
storage.setItem('scroll-y', window.pageYOffset);
}
catch (e) {
return;
}
});
}
catch (e) {
return; // no sessionStorage support
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment