如何根据引荐来源

时间:2017-07-20 05:41:21

标签: javascript redirect url-redirection

我是javascript的新手,我一直在尝试,我一直很享受。

我想尝试将人们重定向到此页面,如果他们不是来自此引荐来源而不是重定向,如果他们是。

但我有一个问题,问题是引用链接动态变化,所以我可以javascript只检查它是否来自这个主链接并忽略其他部分。

例如。

我希望他们一般来自google.com。但谷歌可以将它们引荐到我的网站的唯一方法是通过动态链接,比如google.com/323asda2,并且每次访问链接时代码都会更改。

现在这是我的代码:

<script>
if (document.referrer !== "/^https?:\/\/([^\/]+\.)?google\.com(\/|$)/i") 
{
    alert("** This is a message from the website administration ** \n\n In order for this site to be safe we use bot checker captchas. You will be redirected to our captcha page and you'll be able to access the website after filling that in.")
    window.location.href = "http://websitename.com";       
}
</script>

当他们尝试从谷歌访问该页面时,循环继续,因为链接(后面部分; google.com/323asda2&gt; 323asda2)动态变化。

基本上我希望脚本只检查主链接,而不是其他部分

我希望你理解我的问题,希望你能帮助我!

1 个答案:

答案 0 :(得分:0)

不是尝试匹配整个引荐来源,而是将其保存在变量中并检查字符串是否包含url:

var ref = document.referrer;

if(ref.search(/^https?:\/\/([^\/]+\.)?google\.com(\/|$)/i)){
    // alert here
}
相关问题