检查推荐人 [英] Checking the referrer

查看:154
本文介绍了检查推荐人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用它来检查是否有人来自Reddit,但它不起作用。

I'm using this to check if someone came from Reddit, however it doesn't work.

var ref = document.referrer;
if(ref.match("/http://(www.)?reddit.com(/)?(.*)?/gi"){
    alert('You came from Reddit');
} else {
    alert('No you didn\'t');
}

关于正则表达式的建议也非常受欢迎。

Suggestions on the regular expression are most welcome too.

推荐答案

试试这个:

if (ref.match(/^https?:\/\/([^\/]+\.)?reddit\.com(\/|$)/i)) {
  alert("Came from reddit");
}

正则表达式:

/^           # ensure start of string
 http        # match 'http'
 s?          # 's' if it exists is okay
 :\/\/       # match '://'
 ([^\/]+\.)? # match any non '/' chars followed by a '.' (if they exist)
 reddit\.com # match 'reddit.com'
 (\/|$)      # match '/' or the end of the string
/i           # match case-insenitive

这篇关于检查推荐人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆