Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
防止Javascript函数中的重定向循环_Javascript_Jquery_Redirect - Fatal编程技术网

防止Javascript函数中的重定向循环

防止Javascript函数中的重定向循环,javascript,jquery,redirect,Javascript,Jquery,Redirect,我得到了一个可通过两个URL访问的ressource,例如:mygoodlookingurl.de/nice.jsf和mybadlookingurl.de/bad123454423523413413.jsf 我必须调用一次外观不好的url,然后我想重定向。目前,我使用JQuery的document-ready()-函数并调用cleanUrl(),该函数通过top.location.href=“mygoodlookingurl.de/nice.jsf”重定向 不幸的是,出现了一个无限循环,因为在o

我得到了一个可通过两个URL访问的ressource,例如:mygoodlookingurl.de/nice.jsf和mybadlookingurl.de/bad123454423523413413.jsf

我必须调用一次外观不好的url,然后我想重定向。目前,我使用JQuery的document-ready()-函数并调用cleanUrl(),该函数通过top.location.href=“mygoodlookingurl.de/nice.jsf”重定向

不幸的是,出现了一个无限循环,因为在onload上加载url之后,函数会被一次又一次地调用。怎么办

下面是JS代码(我使用一个技巧来读取带有创建的锚标记的URL):

提前谢谢

更新:在第一次重定向之后,currentUrl不会更新,因此无限循环仍然会发生

function cleanUrl(){
    var cleanedUrl = "https://xxx.xxx.xx:xxx.jsf";
    var currentUrl = window.location.href;
    if ( currentUrl != cleanedUrl){
        top.location.href = cleanedUrl;
    } 
}

只是把重定向放在某种形式的条件测试中

var cleanedUrl = "https://xxx.xxx.xx/xxx.jsf";
if(top.location.href !== cleanedUrl){
    top.location.href = cleanedUrl;
}

谢谢你,杰克,我能同时处理这个问题

return mygoaljsffile.jsf?faces-redirect=true; 

在my Bean方法中。

您不能为此使用服务器端代码吗?问题是:在第一次重定向后,currentUrl没有更新(请参阅文章中更新的代码)。
return mygoaljsffile.jsf?faces-redirect=true;