@@ -71,6 +71,7 @@ var $articleContainer,
7171 // Back/Forward navigation
7272 window . addEventListener ( 'popstate' , function ( ev ) {
7373 ev . preventDefault ( ) ;
74+ ev . stopImmediatePropagation ( ) ; // This helps maintain the correct scroll position in Chrome
7475 navigate ( window . location . href , false ) ;
7576 } ) ;
7677
@@ -240,13 +241,17 @@ function navigate(href, updateLocation) {
240241 return ;
241242 }
242243
243- $articleContainer . scrollTop ( 0 ) ;
244-
245244 // just scroll to hash if possible
246- if ( window . location . hash && href . replace ( / # .* / , '' ) === currentHref . replace ( / # .* / , '' ) ) {
247- scrollToElement ( $ ( window . location . hash ) ) ;
248- return ;
249- }
245+ var currentHrefBase = currentHref . replace ( / # .* / , '' ) ; // This is the current URL without the hash
246+ var hrefBase = href . replace ( / # .* / , '' ) ; // This is the new URL without the hash
247+ if ( currentHrefBase === hrefBase ) {
248+ var hrefHash = href . match ( / # .* / , '' ) || [ ] ; // Match the hash part of the URL, including the hash
249+ scrollToElement ( $ ( hrefHash [ 0 ] ) ) ;
250+ if ( updateLocation !== false ) { // We don’t want to pushState when our popstate listener calls this
251+ window . history . pushState ( { articleScroll : $articleContainer . scrollTop ( ) } , null , href ) ;
252+ }
253+ return ;
254+ }
250255
251256 // clear existing scroll interval if it's still alive
252257 clearInterval ( scrollPositionInterval ) ;
@@ -284,6 +289,10 @@ function navigate(href, updateLocation) {
284289 if ( ! $content . length ) {
285290 window . location . reload ( ) ;
286291 }
292+
293+ // Scroll to the top of the page
294+ $articleContainer . scrollTop ( 0 ) ;
295+
287296 var $article = $content . find ( "article" ) ;
288297 var $breadcrumb = $content . find ( ".breadcrumb" ) ;
289298 var homeLink = $content . find ( ".logo > a" ) . attr ( 'href' ) ;
@@ -424,7 +433,7 @@ function setOnThisPageScroll() {
424433 $ . each ( $headers , function ( index , header ) {
425434 var $header = $ ( header ) ;
426435 var marginTop = parseInt ( $header . css ( 'margin-top' ) ) || 20 ;
427- if ( $header . offset ( ) . top - marginTop - contOffsetTop <= 0 ) {
436+ if ( $header . offset ( ) . top - marginTop - contOffsetTop < 1 ) { // < 1 to allow fractional values
428437 onThisPageTitle = $header . html ( ) ;
429438 }
430439 } ) ;
0 commit comments