2013-03-29 56 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