|
49 | 49 | document.addEventListener("DOMContentLoaded", function() { |
50 | 50 | var currentUrl = window.location.href; |
51 | 51 | if (currentUrl.includes("/wiki/")) { |
52 | | - // Check if the current URL already ends with '/en', to prevent infinite redirection |
53 | | - if (!currentUrl.endsWith("/en")) { |
54 | | - // Extract the base URL and the last segment |
55 | | - var baseUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/') + 1); |
56 | | - var lastSegment = currentUrl.substring(currentUrl.lastIndexOf('/') + 1); |
| 52 | + var urlParts = currentUrl.split("/"); |
| 53 | + var lastSegment = urlParts[urlParts.length - 1]; |
| 54 | + |
| 55 | + if (lastSegment !== 'en') { |
| 56 | + var newUrl = currentUrl.endsWith('/') ? currentUrl + 'en' : currentUrl + '/en'; |
57 | 57 |
|
58 | | - // Check if the last segment is not 'en' |
59 | | - if (lastSegment !== 'en') { |
60 | | - var newUrl = baseUrl + 'en'; |
61 | | - |
62 | | - // Make an XMLHttpRequest to check the existence of the new URL |
63 | | - var xhr = new XMLHttpRequest(); |
64 | | - xhr.open('HEAD', newUrl, true); |
65 | | - xhr.onreadystatechange = function() { |
66 | | - if (xhr.readyState === 4) { |
67 | | - if (xhr.status === 404) { |
68 | | - window.location.href = newUrl; |
69 | | - } else { |
70 | | - document.querySelector('.container').style.display = 'block'; |
71 | | - } |
| 58 | + fetch(newUrl, { method: 'HEAD' }) |
| 59 | + .then(response => { |
| 60 | + if (!response.ok) { |
| 61 | + window.location.href = newUrl; |
72 | 62 | } |
73 | | - }; |
74 | | - xhr.send(); |
75 | | - } else { |
76 | | - document.querySelector('.container').style.display = 'block'; |
77 | | - } |
78 | | - } else { |
79 | | - document.querySelector('.container').style.display = 'block'; |
| 63 | + }) |
| 64 | + .catch(error => console.error('Error checking URL:', error)); |
80 | 65 | } |
81 | | - } else { |
82 | | - document.querySelector('.container').style.display = 'block'; |
83 | 66 | } |
84 | 67 | }); |
85 | 68 | </script> |
|
0 commit comments