Created
April 26, 2017 12:41
-
-
Save lancegliser/5a1c25f42b32439d2fb3f7e822a253cb 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
/** | |
* 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