以下是我网站的 .js 文件的函数,该文件包含在网页的 head 部分,test.html
function test_redirect() {
var urlCurrent = document.URL;
window.location = "http://webanywhere.cs.washington.edu/beta/?starting_url=" + urlCurrent;
}
$.test_redirect();
从 javascript 代码中可以清楚地看出,我正在尝试将打开页面 test.html 的用户重定向到另一个页面,该页面接受我所在的正确页面的 URL 作为查询字符串中的参数并对其执行一些操作。
当重定向的网页加载多次时,就会出现问题。 重定向的页面实际上会打开带有注入(inject)脚本页面的相同 test.html,但重定向会再次开始。
有人可以帮我找出问题所在吗? .
最佳答案
检查引荐来源网址:
function test_redirect() {
if( document.referrer.indexOf('http://webanywhere.cs.washington.edu') < 0 ){
var urlCurrent = window.location.url;
window.location = "http://webanywhere.cs.washington.edu/beta/?starting_url=" + urlCurrent;
}
}
$.test_redirect();
关于javascript - 停止无限重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11529679/