2013-03-29 58 views
0

使用脚本来定位外部网站的URL并为触发对话框提供类。需要排除(白名单)某些HREF。 mailto和tel以及内部URL的例外情况正常,但尝试添加特定的URL名称不起作用。有什么建议么?有更好的方法吗?RegEx创建URL白名单?

jQuery(document).ready(function ($) { 

$.expr[":"].external = function (a) { 
    return !a.href.match(/^mailto\:/) && !a.href.match(/^tel\:/) && !a.href.match(/http\/\/\:\mail\.google\.com/) && a.hostname != location.hostname 
}; 
$("a:external").addClass("ext_link"); 

});

+0

除了有你的斜线和冒号逆转,这似乎很好地工作:http://jsfiddle.net/5bZVh/ –

+0

啊...感谢没收。这确实解决了问题! – Zendiko

回答

1

也许

return !/^(mailto|tel):|http:\/\/mail\.google\.com/.test(a.href) && 
    a.hostname != location.hostname 
+0

谢谢MikeM!真的很喜欢这种简化的方式!运行这个解决方案。 – Zendiko