From d4936227ad39cf227a4d7f59ef029fda9efa97ac Mon Sep 17 00:00:00 2001 From: Eric Hwang Date: Tue, 24 Jan 2023 15:41:18 -0800 Subject: [PATCH] Keep URL hash across page transition, and jump to named element if present --- lib/History.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/History.js b/lib/History.js index e844fea..602b065 100644 --- a/lib/History.js +++ b/lib/History.js @@ -71,9 +71,16 @@ History.prototype._update = function(historyMethod, relativeUrl, render, state, var options = renderOptions(e, path) state.$render = true state.$method = options.method - window.history[historyMethod](state, null, options.url) + window.history[historyMethod](state, null, options.url + (url.hash || '')) currentPath = window.location.pathname + window.location.search if (render) router.render(this, options, e) + // Jump to named element on new page, to mimic browser behavior + if (url.hash) { + var hashElement = document.getElementById(url.hash.substring(1)) + if (hashElement) { + hashElement.scrollIntoView() + } + } } History.prototype.page = function() {