If you consider a URL being external if either the scheme, host or port is different, you could do something like this:
function isExternal(url) {
var match = url.match(/^([^:/?#]+:)?(?://([^/?#]*))?([^?#]+)?(?[^#]*)?(#.*)?/);
if (typeof match[1] === "string" && match[1].length > 0 && match[1].toLowerCase() !== location.protocol) return true;
if (typeof match[2] === "string" && match[2].length > 0 && match[2].replace(new RegExp(":("+{"http:":80,"https:":443}[location.protocol]+")?$"), "") !== location.host) return true;
return false;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…