我有一个可通过两个 url 获得的资源,例如:mygoodlookingurl.de/nice.jsf 和 mybadlookingurl.de/bad123454423523413413.jsf
我必须调用一次看起来很糟糕的 url,然后我想重定向。目前我使用了 JQuery 的 document-ready()-function 并调用 cleanUrl() ,它通过 top.location.href = "mygoodlookingurl.de/nice.jsf"; 重定向。
不幸的是,发生了无限循环,因为在 onload 加载 url 后,该函数被一次又一次地调用。该怎么办?
这是 JS 代码(我正在使用一种技巧来读取带有创建的锚标记的 URL):
function cleanUrl(){
var url = document.url;
var a = "";
a = document.createElement( 'a' );
a.href = url;
if ( a.pathname == "/xxx/undefined"){
var cleanedUrl = "https://xxx.xxx.xx/xxx.jsf";
top.location.href = cleanedUrl;
}
}
提前致谢。
更新:第一次重定向后 currentUrl 未更新,因此仍会发生无限循环。
function cleanUrl(){
var cleanedUrl = "https://xxx.xxx.xx:xxx.jsf";
var currentUrl = window.location.href;
if ( currentUrl != cleanedUrl){
top.location.href = cleanedUrl;
}
}