New URL redirection

This commit is contained in:
2025-11-30 22:30:46 +08:00
parent 83aaf333d6
commit 48fc7287a6
9 changed files with 102 additions and 153 deletions

47
docs/js/route.js Normal file
View File

@@ -0,0 +1,47 @@
let isRouting = false;
function route() {
if (isRouting) return;
isRouting = true;
// Define route tables
const routeHandlers = {
'index.html': handleIndex,
};
const currentHash = window.location.hash.replace(/^#\/?/, '');
if (!currentHash || currentHash === "/") {
const browserLang = getBrowserLanguage();
window.location.href = `#/${browserLang}/index.html`;
setTimeout(() => {
isRouting = false;
}, 50);
return;
}
const match = currentHash.match(/^([^/]+)\/([^/]+\.html)(?:$|\?)/);
if (!match) {
handle404Page();
setTimeout(() => {
isRouting = false;
}, 50);
return;
}
const [_, lang, page] = match;
setHtmlLangAttribute(lang);
const handler = routeHandlers[page];
if (handler) {
handler(lang);
} else {
handle404Page();
}
setTimeout(() => {
isRouting = false;
}, 50);
}
window.addEventListener('hashchange', route);
window.addEventListener('popstate', route);