RegEx表达式或jQuery选择器不匹配“外部”链接在href [英] RegEx expression or jQuery selector to NOT match "external" links in href

查看:110
本文介绍了RegEx表达式或jQuery选择器不匹配“外部”链接在href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个覆盖链接行为的jQuery插件,允许Ajax加载页面内容。用 $(document).on('click','a',function(){});



但是我只希望它适用于不同于这些链接的链接(Ajax加载不适用于它们,所以像这样的链接需要正常运行):

  target =_ blank//新的浏览器窗口
target =_ self//强制替换当前窗口特定于我的插件)
href =#...//书签链接(已加载页面)。
href =afs:// ...// AFS文件访问。
href =cid:// ...// // MIME正文部分的内容标识符。
href =file:// ...//指定本地可访问驱动器中文件的地址。
href =ftp:// ...//使用Internet文件传输协议(FTP)来检索文件。
href =http:// ...//最常用的访问方法。
href =https:// ...//提供某种级别的传输安全性
href =mailto:// ...//打开一个电子邮件程序。
href =mid:// ...//电子邮件的消息标识符。
href =news:// ...// Usenet新闻组。
href =x-exec:// ...//可执行程序。
href =http://AnythingNotHere.com//外部链接



示例代码:



  $(document).on('click','a:not([target =_ blank])' ,函数(){
var $ this = $(this);
if('href'的一些额外检查){
//执行ajax加载并停止默认行为
返回false;
}
//允许链接正常工作
});



问:



有一种方法可以轻松检测出只能在当前网站内导航的所有本地链接,不包括上述所有变化。

:这是针对MVC 5 Razor网站的,因此绝对不会发生网站网址。

解决方案

使用选择器:

  $('a:not([href * =://],[target = _blank],[href ^ =#],[href ^ =mailto:])')

http://jsfiddle.net/mblase75/Pavg2/



请注意, mailto:是正确的,而不是 mailto:// 原始问题。




出于彻底性的考虑,以下内容也会捕获使用绝对URL的内部链接( http://the-domain-you-are-on-now.com/whatever.html ):

  $('一个:否([HREF * = ://],[目标= _blank],[HREF ^ = #],[HREF ^ = 的mailto:]),A [ href ^ =''+ location.protocol +'//'+ location.hostname +']')

http://jsfiddle.net/mblase75/Pavg2/4/


I have a jQuery plugin that overrides link behavior, to allow Ajax loading of page content. Simple enough with a delegated event like $(document).on('click','a', function(){});.

but I only want it to apply to links that are not like these ones (Ajax loading is not applicable to them, so links like these need to behave normally):

target="_blank"      // New browser window
target="_self"       // Force replacement of current window (specific to my plugins)
href="#..."          // Bookmark link (page is already loaded). 
href="afs://..."     // AFS file access. 
href="cid://..."     // Content identifiers for MIME body part.
href="file://..."    // Specifies the address of a file from the locally accessible drive.
href="ftp://..."     // Uses Internet File Transfer Protocol (FTP) to retrieve a file.
href="http://..."    // The most commonly used access method. 
href="https://..."   // Provide some level of security of transmission 
href="mailto://..."  // Opens an email program.
href="mid://..."     // The message identifier for email.
href="news://..."    // Usenet newsgroup.
href="x-exec://..."  // Executable program.
href="http://AnythingNotHere.com"  // External links

Sample code:

$(document).on('click', 'a:not([target="_blank"])', function(){
    var $this = $(this);
    if ('some additional check of href'){
        // Do ajax load and stop default behaviour
        return false;
    }
    // allow link to work normally
});

Q:

Is there a way to easily detect all "local links" that would only navigate within the current website? excluding all the variations mentioned above.

Note: This is for an MVC 5 Razor website, so absolute site URLs are unlikely to occur.

解决方案

I might use the selector:

$('a:not([href*="://"],[target="_blank"],[href^="#"],[href^="mailto:"])')

http://jsfiddle.net/mblase75/Pavg2/

Note that mailto: is correct, not mailto:// as in your original question.


In the interest of thoroughness, the following will also catch internal links that use absolute URLs (http://the-domain-you-are-on-now.com/whatever.html):

$('a:not([href*="://"],[target="_blank"],[href^="#"],[href^="mailto:"]),a[href^="'+location.protocol+'//'+location.hostname+'"]')

http://jsfiddle.net/mblase75/Pavg2/4/

这篇关于RegEx表达式或jQuery选择器不匹配“外部”链接在href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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