【问题标题】:Problems with document.referrerdocument.referrer 的问题
【发布时间】:2015-04-23 03:53:36
【问题描述】:

我在某些网站上使用 document.referrer 时遇到了一些问题,甚至是使用 HTTP 协议的参考网站。

  1. 什么情况下document.referrer可能为空(HTTPS转HTTP除外)?

  2. 获取url仅引用document.referrer的最佳方法是什么?

  3. 在我的网站中,document.referrer 是空的,但是当我查看 Analytics 网站时出现了引用。

【问题讨论】:

    标签: javascript referrer


    【解决方案1】:

    document.referrernot empty 如果您的网站 URL 是从其他网站点击的,例如从 google 搜索、从 facebook、twitter 等...

    所以,如果有人从书签打开您的网站 o 直接输入您的网站 URL,document.referrer 就是 empty

    让我们看看这些例子:

    点击这个链接http://www.w3schools.com/jsref/prop_doc_referrer.asp可以看到document.referrer是http://stackoverflow.com/questions/28646433/problems-with-document-referrer

    如果你在谷歌搜索HTML DOM referrer Property,document.referrer 将是http://google.es/...

    如果你复制 http://www.w3schools.com/jsref/prop_doc_referrer.asp 在浏览器中打开新标签并粘贴,document.referrer 将是 empty

    【讨论】:

    • 好一个。为什么我给ctrl+F5F5 键时document.referrer 不为空。
    • 嗯,CTRL + F5F5 只刷新当前文档内容,而不是 HTTP 元数据,如 headers、cookie 等……这里是完整的解释 http://stackoverflow.com/a/385384/3710490
    • 对!当我重新加载同一页面时重置 document.referrer 的任何解决方法。艰难,我看到它是一个只读道具,按照文档手动设置。谢谢
    • 不。只有你已经设计了一个服务器,在Response对象覆盖http头
    • 无法依赖推荐人信息,即可以在某些浏览器设置中关闭。它也可以由客户端显式设置为任何内容,例如 cURL:cyberciti.biz/faq/…
    【解决方案2】:
    const referrerPage = document.referrer;
    
    if ((referrerPage.indexOf(window.location.href) > -1) !== true) { // If referral is same page then page will not redirect.
        if (referrerPage.indexOf('viewplans') <= -1) {
            window.location.href = '/';
            return false;
        }
    }
    

    viewplans 是页面名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-07
      • 2013-07-16
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多