Skip to content

Commit

Permalink
Merge #60 - Attempt to reload current page in newly-selected version
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed Apr 6, 2024
2 parents 1aa22bf + 0447854 commit 865cbaf
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 865cbaf

Please sign in to comment.