Skip to content

Instantly share code, notes, and snippets.

@lancegliser
Created April 26, 2017 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancegliser/5a1c25f42b32439d2fb3f7e822a253cb to your computer and use it in GitHub Desktop.
Save lancegliser/5a1c25f42b32439d2fb3f7e822a253cb to your computer and use it in GitHub Desktop.
/**
* Provides data that can be used to ensure location.reload() behaves the same
* with regard to x and y position across browsers
*
* Introduced because IE goes scroll 0 using window.location.reload, but stays in place for F5.
*
* @param {boolean} [forcedReload]
* Causes the page to always be reloaded from the server if true
*
* @see restoreToSamePosition
*
* NOTE: I tried overriding window.location.reload - browsers don't allow it
*/
function reloadAtSamePosition(forcedReload){
if( !window.sessionStorage ){
window.location.reload(forcedReload);
}
sessionStorage.setItem('window.position', jQuery(window).scrollTop());
window.location.reload(forcedReload);
}
/**
* Ensures location.reload() behaves the same
* with regard to x and y position across browsers
*
* Introduced because IE is a bitch
*
* @see reloadAtSamePosition
*/
function restoreToSamePosition(){
// Someday this could be done via cookies.. but the support is good enough now without
if( !window.sessionStorage ){
return;
}
var item = sessionStorage.getItem('window.position');
if( !item ){
return;
}
jQuery(window).scrollTop(item);
sessionStorage.removeItem('window.position');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment