【问题标题】:jQuery Warning Upon Leaving Website离开网站时的 jQuery 警告
【发布时间】:2013-10-29 08:03:48
【问题描述】:

我有以下代码,除了它还捕获同一域中的子域之外,它的效果很好。例如,如果在 example.ca 上,我想去 sub.example.ca 它不会让我。我了解该网站在技术上有所不同,但我不需要这些页面的警告。

同样在站点 sub.example.ca 上(因为它使用大多数相同的文件)触发每个内部链接,因为内部链接现在是 sub.example.ca,这真的很烦人。

我可以使用 bypass 变量,但不想将它添加到我创建的每个菜单和页面中。懒惰是最好的。哈哈。

$(document).ready(function() {
  $("a").click(function() {
    var href = this.href;
    var ourDomainRegex = /^https?:\/\/(www[.])?example[.]ca/;
    // those seeking flexibility, consider this: 
    // new RegExp('^https?:\\/\\/(www[.])?'  +  ourDomain)
    if (href.indexOf("http") === 0   &&   ! ourDomainRegex.test(href) )
    {
      if($(this).attr('rel')!='pass') {
          this.href = "/leave.php?p=" + href;
      }
    }
  });
});

我将如何让它适用于子域。谢谢!

【问题讨论】:

标签: jquery regex web dns


【解决方案1】:

您可以让您的正则表达式允许任何子域:

/^https?:\/\/([a-z0-9-]+[.])*example[.]ca/

【讨论】:

  • 这似乎仍然不起作用,并且当链接指向子域或子域站点上有任何内部链接时仍会重定向到警告。 :(
猜你喜欢
  • 2012-06-15
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
  • 2013-10-17
  • 2018-12-31
相关资源
最近更新 更多