From 0447854ba8acb4272917efe973611299093e2017 Mon Sep 17 00:00:00 2001 From: Andrew Paxley Date: Wed, 11 Oct 2023 11:39:50 +1300 Subject: [PATCH] ENH attempt to reload current page in newly-selected version --- src/Resources/themes/default/doctum.js.twig | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Resources/themes/default/doctum.js.twig b/src/Resources/themes/default/doctum.js.twig index a46f8f9b..6f9a3fac 100644 --- a/src/Resources/themes/default/doctum.js.twig +++ b/src/Resources/themes/default/doctum.js.twig @@ -76,8 +76,20 @@ var Doctum = { {# Enable the version switcher #} var versionSwitcher = document.getElementById('version-switcher'); if (versionSwitcher !== null) { - versionSwitcher.addEventListener('change', function () { - window.location = this.value; + var currentVersion = versionSwitcher.options[versionSwitcher.selectedIndex].dataset.version; + versionSwitcher.addEventListener('change', function (event) { + var targetVersion = event.target.options[event.target.selectedIndex].dataset.version; + var candidateUrl = window.location.pathname.replace(currentVersion, targetVersion); + // Check if the page exists before redirecting to it + var testRequest = new XMLHttpRequest(); + testRequest.open('HEAD', candidateUrl, false); + testRequest.send(); + if (testRequest.status < 200 || testRequest.status > 399) { + window.location = candidateUrl; + } else { + // otherwise reroute to the home page of the new version + window.location = this.value; + } }); } {% endif %}