Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,27 @@ const SHOULD_DEBUG = window.DEBUG;
*/
export default function findLinksAndSetTargets() {
const links = document.getElementsByTagName('a');
const baseDocumentHost = document.location.host;
const baseDocumentHostWithSubdomain= `www.${baseDocumentHost}`;
const baseDocHost = document.location.host;
const baseDocHostWithSubdomain= `www.${baseDocHost}`;

[ ...links ].forEach( function setTarget(link) {
if ( ! link.href) {
return false;
}

const isMailto = (link.href.indexOf('mailto:') === 0);
if ( ! link.href) {
return false;
}

const isInternalLink = link.host === baseDocumentHost || link.host === baseDocumentHostWithSubdomain
const isMailto = ( link.href.indexOf('mailto:') === 0 );
const isSameHost = link.host === baseDocHost || link.host === baseDocHostWithSubdomain

if (!isInternalLink || isMailto ) {
if (link.target !== '_blank') {
link.target = '_blank';
if (SHOULD_DEBUG) {
console.debug(`Link ${link.href} now opens in new tab`);
}
}
if (link.target === '_blank') {
link.setAttribute('aria-description', 'Opens in new window.');
}
if (typeof setTargetCallback === 'function') {
setTargetCallback( link );
if ( ! isSameHost || isMailto ) {
if ( link.target !== '_blank') {
link.target = '_blank';
if (SHOULD_DEBUG) {
console.debug(`Link ${link.href} now opens in new tab`);
}
}
if ( link.target === '_blank') {
link.setAttribute('aria-description', 'Opens in new window.');
}
}
});
}