检查引荐来源网址

发布于 2024-08-16 23:05:43 字数 245 浏览 5 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

装迷糊 2024-08-23 23:05:43

试试这个:

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

Try this:

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

The regexp:

/^           # 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
长发绾君心 2024-08-23 23:05:43

关闭您的 if 父项...

Close your if paren...

眼睛会笑 2024-08-23 23:05:43

我一直在使用正则表达式的替代方法,在引荐来源网址中查找域编辑

if (document.referrer.indexOf('reddit.com') >= 0) { alert('They came from Reddit.com'); }

:正如 thekingoftruth 指出的那样,如果 reddit.com 包含在 URL 参数中,则该方法不起作用,因此我对其进行了一些扩展。我还添加了 toLowerCase(),因为我在上面的正则表达式中发现了这一点。

if (document.referrer.indexOf('?') > 0){
    if (document.referrer.substring(0,document.referrer.indexOf('?')).toLowerCase().indexOf('reddit.com') >= 0){
    alert('They came from Reddit');
    }
} else {
    if (document.referrer.toLowerCase().indexOf('reddit.com') > 0){
            alert('They came from Reddit');
    }
}

I've been using an alternative to RegEx by looking for the domain in the referrer

if (document.referrer.indexOf('reddit.com') >= 0) { alert('They came from Reddit.com'); }

EDIT: As thekingoftruth points out that doesn't work if reddit.com is included in an URL parameter so I've extended it a little. I've also added toLowerCase() as I spotted that in the RegExp above.

if (document.referrer.indexOf('?') > 0){
    if (document.referrer.substring(0,document.referrer.indexOf('?')).toLowerCase().indexOf('reddit.com') >= 0){
    alert('They came from Reddit');
    }
} else {
    if (document.referrer.toLowerCase().indexOf('reddit.com') > 0){
            alert('They came from Reddit');
    }
}
捶死心动 2024-08-23 23:05:43

试试这个:

ref.match(new RegExp("^http://(www\\.)?reddit\\.com/", "i"))

或者:

ref.match(/^http:\/\/(www\.)?reddit\.com\//i)

Try this:

ref.match(new RegExp("^http://(www\\.)?reddit\\.com/", "i"))

Or:

ref.match(/^http:\/\/(www\.)?reddit\.com\//i)
聽兲甴掵 2024-08-23 23:05:43

我会用这个,这不是一个更简单的方法吗?

  var referral= document.refferer;
   If(referral.includes("www.reddit.com"){
  alert("you came from reddit");
   }
     else{
    alert("you didn't come from reddit");
       {

I would use this, wouldn't it be a lesser and simply way?

  var referral= document.refferer;
   If(referral.includes("www.reddit.com"){
  alert("you came from reddit");
   }
     else{
    alert("you didn't come from reddit");
       {
耳钉梦 2024-08-23 23:05:43

使用 var ref = document.referer;
// 一个 R 而不是两个

Use var ref = document.referer;
// ONE R instead of TWO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文