我有一个示例页面,让'说testpage.pl当我选择英文版时,GET参数被添加到URL,如/?language=en
。
之后,当我点击菜单位置时,它们都是英文版,所以一切正常。
但是如果我想在浏览器中粘贴网址后直接使用英文版的子页面,例如
http://testpage.pl/wyjazdy-i-przyjazdy/erasmus-incoming-staff/accommodation.html)
打开波兰语版本。所以我做了一个简单的重定向函数,如下所示,但它在第一次启动后进入循环。此函数重定向到同一页面,但在尝试使用GET参数?language=en
如何解决这个问题?
function cleanUrl() {
window.location = "http://testpage.pl/?language=en";
var cleanedUrl = "http://testpage.pl/wyjazdy-i-przyjazdy/erasmus-incoming-staff/accommodation.html";
var currentUrl = window.location.href;
if (currentUrl !== cleanedUrl) {
window.location = cleanedUrl;
}
}
cleanUrl();
答案 0 :(得分:0)
您正在更新第一行函数中的url,这会导致代码无限循环。删除该行或移动到其他功能进行修复
function cleanUrl() {
var cleanedUrl = "http://testpage.pl/wyjazdy-i-przyjazdy/erasmus-incoming-staff/accommodation.html";
var currentUrl = "http://testpage.pl/?language=en";
if (currentUrl !== cleanedUrl) {
window.location = cleanedUrl;
}
}
cleanUrl();
答案 1 :(得分:0)
将window.location
作业保留为最后一次操作。
function cleanUrl() {
var enUrl = "http://testpage.pl/?language=en";
var cleanedUrl = "http://testpage.pl/wyjazdy-i-przyjazdy/erasmus-incoming-staff/accommodation.html";
var currentUrl = window.location.href;
if( currentUrl !== cleanedUrl ) { enUrl = cleanedUrl; }
window.location = enUrl;
}