我有一个jQuery插件,可以重写链接的行为,以允许页面内容的Ajax加载。使用委托事件很简单,例如$(document).on('click','a', function(){})
。
但是我只想将其应用于不像这些链接的链接(Ajax加载对它们不适用,所以这样的链接需要正常工作):
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
样例代码:
$(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
});
问:
有没有一种简单的方法可以检测出所有“本地链接”,即只在当前网站内导航的链接呢?不包括上述所有变体。
注意:这是一个MVC 5 Razor网站,因此绝对网站URL不太可能发生。