Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
kailasnadh790 committed Dec 7, 2023
1 parent 07b8b33 commit 93c8367
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cigaradvisor/blocks/imagecta/imagecta.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export default async function decorate(block) {
anchor.append(picture);
const link = block.querySelector('a').getAttribute('href');
anchor.setAttribute('href', link);
anchor.setAttribute('target', isExternal(link));
anchor.setAttribute('target', isExternal(link) ? '_blank' : '_self');
anchor.setAttribute('title', img.alt);
children.push(anchor);
block.replaceChildren(...children);
}
11 changes: 7 additions & 4 deletions cigaradvisor/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ export function decorateMain(main) {
buildTwoColumnGrid(main);
}

export function isExternal(url) {
const anchor = document.createElement('a');
anchor.href = url;
return (anchor.hostname !== window.location.hostname) ? '_blank' : '_self';
export function isExternal(path) {
try {
const url = new URL(path);
return window.location.hostname !== url.hostname;
} catch (error) {
return false;
}
}

/**
Expand Down

0 comments on commit 93c8367

Please sign in to comment.