Prevent the scroll restoration caused by the
popstate
event or back/forward buttons. Demo
Warning: it works in Chrome, Safari, and Firefox 46+, but there's no IE/Edge support yet.
The polyfill has no API. Once included, just use the official API as described on Chrome Developers:
// Prevent the scroll, always
history.scrollRestoration = 'manual';
// Stop preventing the scroll
history.scrollRestoration = 'auto';
Pick your favorite:
<script src="dist/scroll-restoration-polyfill.browser.js"></script>
npm install --save scroll-restoration-polyfill
require('scroll-restoration-polyfill');
import 'scroll-restoration-polyfill';
- Specification: http://majido.github.io/scroll-restoration-proposal/history-based-api.html
- Native compatibility table: https://developer.mozilla.org/en/docs/Web/API/History#Browser_compatibility
- Chrome Developers post: https://developers.google.com/web/updates/2015/09/history-api-scroll-restoration
Two lightweight dependencies, already included in scroll-restoration-polyfill
's
MIT © Federico Brigante