javascript - 停止无限重定向

标签 javascript jquery html

以下是我网站的 .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/

相关文章:

Javascript 将 rel 属性分配给变量

javascript - 使用 Typescript 将 prop 渲染为字符串或组件

javascript - Globalize.js 十进制数字验证的值错误

E11 上的 JavaScript readAsBinaryString 函数

javascript - 数据仅在警报后可用

javascript - jQuery 表单选择器 :input is excluding the :checked in chrome and firefox

jquery - 在另一个 html 对象中添加 html 对象

java - Java 小程序加载失败时如何显示消息?

javascript - 取消 onbeforeunload 事件处理程序?

javascript - 在响应式网格站点中拥有全屏宽度背景