Skip to content

Commit 0447854

Browse files
committed
ENH attempt to reload current page in newly-selected version
1 parent 2ca5517 commit 0447854

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Diff for: src/Resources/themes/default/doctum.js.twig

+14-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,20 @@ var Doctum = {
7676
{# Enable the version switcher #}
7777
var versionSwitcher = document.getElementById('version-switcher');
7878
if (versionSwitcher !== null) {
79-
versionSwitcher.addEventListener('change', function () {
80-
window.location = this.value;
79+
var currentVersion = versionSwitcher.options[versionSwitcher.selectedIndex].dataset.version;
80+
versionSwitcher.addEventListener('change', function (event) {
81+
var targetVersion = event.target.options[event.target.selectedIndex].dataset.version;
82+
var candidateUrl = window.location.pathname.replace(currentVersion, targetVersion);
83+
// Check if the page exists before redirecting to it
84+
var testRequest = new XMLHttpRequest();
85+
testRequest.open('HEAD', candidateUrl, false);
86+
testRequest.send();
87+
if (testRequest.status < 200 || testRequest.status > 399) {
88+
window.location = candidateUrl;
89+
} else {
90+
// otherwise reroute to the home page of the new version
91+
window.location = this.value;
92+
}
8193
});
8294
}
8395
{% endif %}

0 commit comments

Comments
 (0)