Last active
May 23, 2018 01:24
-
-
Save hideya/8f27d501c2dbe68660756a88f740b5f8 to your computer and use it in GitHub Desktop.
This file contains 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
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