description and source-codefunction isLocal(event, anchor, lookForHash) {
event || (event = {})
// Skip modifier events
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
return null
}
// Skip non-primary clicks
if (isSecondaryButton(event)) {
return null
}
// If we have an anchor but its not an A tag
// try to find the closest one
if (anchor && !isA(anchor)) {
anchor = closestA(anchor)
}
// Only test anchor elements
if (!anchor || !isA(anchor)) {
return null
}
// Dont test anchors with target=_blank
if (anchor.target === '_blank') {
return null
}
// IE9 doesn't put a leading slash on anchor.pathname [1]
var aPathname = normalizeLeadingSlash(anchor.pathname)
var wPathname = normalizeLeadingSlash(window.location.pathname)
var aHost = anchor.host
var aPort = anchor.port
var wHost = window.location.host
var wPort = window.location.port
// In some cases, IE will have a blank host property when the href
// is a relative URL. We can check for relativeness via the achor's
// href attribute and then set the anchor's host to the window's host.
if (aHost === '' &&
'attributes' in anchor &&
'href' in anchor.attributes &&
'value' in anchor.attributes.href &&
isRelativeUrl(anchor.attributes.href.value)) {
aHost = wHost
}
// Some browsers (Chrome 36) return an empty string for anchor.hash
// even when href="#", so we also check the href
var aHash = anchor.hash || (anchor.href.indexOf('#') > -1 ? '#' + anchor.href.split('#')[1] : null)
var inPageHash
// Window has no port, but anchor has the default port
if (!wPort && aPort && (aPort === '80' || aPort === '443')) {
// IE9 sometimes includes the default port (80 or 443) on anchor.host
// so we append the default port to the window host in this case
// so they will match for the host equality check [1]
wHost += ':' + aPort
aHost += aHost.indexOf(aPort, aHost.length - aPort.length) === -1 ? ':' + aPort : '' // [3]
}
// Hosts are the same, its a local link
if (aHost === wHost) {
// If everything else is the same
// and hash exists, then it is an in-page hash [2]
inPageHash =
aPathname === wPathname &&
anchor.search === window.location.search &&
aHash
if (lookForHash === true) {
// If we are looking for the hash then this will
// only return a truthy value if the link
// is an *in-page* hash link
return inPageHash
} else {
// If this is an in page hash link
// then ignore it because we werent looking for hash links
return inPageHash
? null
: aPathname + (anchor.search || '') + (aHash || '')
}
}
return null
}