From 09e7aebcd8f42dd0542b848767c6c2e8d8b5d8f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Sun, 12 Feb 2023 14:10:55 +0100 Subject: [PATCH] nav: fix initial positioning on content pages #476 --- static/js/search.js | 6 +++--- static/js/theme.js | 24 +++++++++++++----------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/static/js/search.js b/static/js/search.js index c5ff45f365..436ee653d6 100644 --- a/static/js/search.js +++ b/static/js/search.js @@ -65,7 +65,7 @@ function triggerSearch(){ // with normal pages, this is handled by the 'pagehide' event, but this // doesn't fire in case of pushState, so we have to do the same thing // here, too - state.contentScrollTop = elc.scrollTop; + state.contentScrollTop = +elc.scrollTop; window.history.pushState( state, '', url ); } } @@ -239,9 +239,9 @@ function searchDetail( value ) { // by a browser history operation, it simply does nothing var state = window.history.state || {}; state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} ); - if( state.contentScrollTop ){ + if( state.hasOwnProperty( 'contentScrollTop' ) ){ window.setTimeout( function(){ - elc.scrollTop = state.contentScrollTop; + elc.scrollTop = +state.contentScrollTop; }, 10 ); return; } diff --git a/static/js/theme.js b/static/js/theme.js index 4bd8c1b44e..97f7d6b720 100644 --- a/static/js/theme.js +++ b/static/js/theme.js @@ -819,7 +819,7 @@ function initScrollPositionSaver(){ function savePosition( event ){ var state = window.history.state || {}; state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} ); - state.contentScrollTop = elc.scrollTop; + state.contentScrollTop = +elc.scrollTop; window.history.replaceState( state, '', window.location ); }; window.addEventListener( 'pagehide', savePosition ); @@ -846,21 +846,23 @@ function scrollToPositions() { var state = window.history.state || {}; state = ( typeof state === 'object') ? state : {}; - if( state.contentScrollTop !== undefined ){ + if( state.hasOwnProperty( 'contentScrollTop' ) ){ window.setTimeout( function(){ - elc.scrollTop = state.contentScrollTop; + elc.scrollTop = +state.contentScrollTop; }, 10 ); return; } - var searchValue = sessionStorage.getItem( baseUriFull+'search-value' ); - var found = elementContains( searchValue, elc ); - var searchedElem = found.length && found[ 0 ]; - if( searchedElem ){ - searchedElem.scrollIntoView( true ); - var scrolledY = window.scrollY; - if( scrolledY ){ - window.scroll( 0, scrolledY - 125 ); + var search = sessionStorage.getItem( baseUriFull+'search-value' ); + if( search && search.length ){ + var found = elementContains( search, elc ); + var searchedElem = found.length && found[ 0 ]; + if( searchedElem ){ + searchedElem.scrollIntoView( true ); + var scrolledY = window.scrollY; + if( scrolledY ){ + window.scroll( 0, scrolledY - 125 ); + } } return; }