【问题标题】:Show bootstrap alert on specific referrer ref=example在特定的引荐来源网址上显示引导警报 ref=example
【发布时间】:2021-04-16 21:10:13
【问题描述】:

如果我从特定推荐人那里获得访问者,我会尝试显示正常警报(引导程序的标准)。我有我的警报

<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Bold</strong> This is some text
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>

我有这个作为触发器

var ref = document.referrer;
if (ref.match(/^https?:\/\/([^\/]+\.)?example\.com\/\?ref=example(\/|$)/i)) {
('.alert').alert();
}

不幸的是,这不起作用,我不是正则表达式专家,所以我真的不知道我哪里出错了。感谢您的帮助!

【问题讨论】:

  • ref 字符串是什么?
  • 这将是 example.com/?ref=example(example.com 将是我的登录页面,example 是引荐来源)
  • 一开始没有https://?然后,将其从正则表达式中删除。否则,regex works.

标签: javascript regex twitter-bootstrap url referrer


【解决方案1】:

由于? 不能出现在协议或域中,您可以简单地使用:

var ref = document.referrer;
if (ref.match(/\?ref=example\b/)) {
    ('.alert').alert();
}

末尾的\b 涵盖了您可能有其他参数的情况。如果 ref 参数不是第一个,您可以使用:/[\&amp;\?]ref=example\b/

如果您还想测试域名,请使用此正则表达式:

/^(https?:\/\/)?[^\/]*?\bexample.com\/\?ref=example\b/

【讨论】:

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