对于我正在从事的项目,我必须基于cookie将页面重定向到正确的语言环境版本。我想做的是读取init上的cookie,检查您是否在正确的语言环境页面上,如果没有,请重定向到正确的页面。
代码示例:
if (window.location.pathname == “/website/nl.html” && locale == “fr”) {
let redirectUrl = window.location.origin + window.location.pathname.replace(‘nl’, ‘fr’);
redirectUrl = window.location.origin + window.location.pathname.replace(‘nl’, ‘fr’);
console.log(redirectUrl);
// Output: http://localhost:4502/website/fr.html
window.location = redirectUrl;
// Page doesn't redirect
}
if (window.location.pathname == “/website/fr.html” && locale == “nl”) {
let redirectUrl = window.location.origin + window.location.pathname.replace(‘fr’, ‘nl’);
redirectUrl = window.location.origin + window.location.pathname.replace(‘nl’, ‘fr’);
console.log(redirectUrl);
// Output: http://localhost:4502/website/nl.html
window.location = redirectUrl;
// Page doesn't redirect
}
基本上,redirectUrl包含正确的值,但是page-url不变,并且显示空白页。刷新后,您将返回第一个不正确的页面。
编辑:我尝试使用window.location.href = ...
和window.location.replace(...)