Skip to content

Commit

Permalink
ENH attempt to reload current page in newly-selected version
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewandante committed Oct 11, 2023
1 parent 2ca5517 commit 0447854
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Resources/themes/default/doctum.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down

0 comments on commit 0447854

Please sign in to comment.