【问题标题】:Code redirect in javascript from a 302来自 302 的 javascript 中的代码重定向
【发布时间】:2018-07-18 13:58:27
【问题描述】:

此代码适用于 Google Chrome,但不适用于 IE 和 Firefox。 网站“somesite.org”向我发送 302(重定向)。 当它来自特定站点 (somesite.org) 时,我想重定向到我站点中的特定文件。

代码:

var url = document.referrer; 

if (url.includes("somesite.org")) { 
    window.location.href = "http://forthisfile.com/this.html"
}

【问题讨论】:

标签: javascript


【解决方案1】:

IE 不支持任何形式的包含,请参阅MDN reference page

也就是说,另一种方法是检查哈希:

if(window.location.hash.indexOf("?") >= 0) {
    ...
}

所以在你的情况下:

  if(window.location.hash.indexOf("somesite.org") > -1 ){

      window.location = "http://forthisfile.com/this.html" ;
    }

【讨论】:

  • 在 Chrome 中运行良好,与之前在 IE 和 Firefox 中相同。我看到IE和Firefox中的referer是空白的,与chrome不同。
【解决方案2】:

这应该可以工作

var url = document.referrer;

    if(url.indexOf("somesite.org") > -1 ){

      window.location = "http://forthisfile.com/this.html" ;
    }

【讨论】:

  • 也只能在 Chrome 中使用。在 IE、firefox 和 edge 中什么都没有。
  • @BRS ,我在 Firefox 中尝试过。它按预期工作。您看到任何具体错误?
  • 如果网站有重定向,我无法将其发送到特定页面,只能使用谷歌浏览器。如果是链接,则所有浏览器都可以使用这两种代码。没有具体错误。
  • 对不起,在 IE 中我有这个错误:对象不支持属性或方法“包含”
  • @BRS,在这种情况下,您可能想尝试 'indexOf' 属性。更新了相同的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多