diff --git a/src/PowderCoating.Web/wwwroot/js/item-wizard.js b/src/PowderCoating.Web/wwwroot/js/item-wizard.js index cc83999..86fdf46 100644 --- a/src/PowderCoating.Web/wwwroot/js/item-wizard.js +++ b/src/PowderCoating.Web/wwwroot/js/item-wizard.js @@ -91,13 +91,23 @@ document.addEventListener('DOMContentLoaded', () => { ownerForm.addEventListener('submit', writeHiddenFields, { capture: true }); // Save scroll position before the form causes a full-page reload so we can - // restore it after the server redirects back to this page. Key is path-specific - // so navigating away and back doesn't restore a stale position. + // restore it after the server redirects back to this page on a validation error. + // Key is path-specific; cleared on pagehide unless we're leaving via a submit so + // a fresh navigation to this page never restores a stale position. const scrollKey = 'wizardScrollY:' + location.pathname; + let wizardSubmitting = false; ownerForm.addEventListener('submit', () => { + wizardSubmitting = true; sessionStorage.setItem(scrollKey, String(Math.round(window.scrollY))); }, { capture: true }); + // If the page unloads for any reason other than our own form submit (e.g. the + // user clicks a nav link or the server redirects to a success page), discard the + // saved position so it doesn't fire on the next fresh visit. + window.addEventListener('pagehide', () => { + if (!wizardSubmitting) sessionStorage.removeItem(scrollKey); + }); + // Restore on load — fire after layout is painted so scrollTo lands correctly. const savedY = sessionStorage.getItem(scrollKey); if (savedY !== null) {