diff --git a/cigaradvisor/blocks/imagecta/imagecta.js b/cigaradvisor/blocks/imagecta/imagecta.js index ee980e02..f3d8d444 100644 --- a/cigaradvisor/blocks/imagecta/imagecta.js +++ b/cigaradvisor/blocks/imagecta/imagecta.js @@ -1,3 +1,5 @@ +import { isExternal } from '../../scripts/scripts.js'; + export default async function decorate(block) { const children = []; const anchor = document.createElement('a'); @@ -7,7 +9,9 @@ export default async function decorate(block) { picture.style.paddingBottom = `${ratio}%`; anchor.style.maxWidth = `${img.width}px`; anchor.append(picture); - anchor.setAttribute('href', block.querySelector('a').getAttribute('href')); + const link = block.querySelector('a').getAttribute('href'); + anchor.setAttribute('href', link); + anchor.setAttribute('target', isExternal(link)); children.push(anchor); block.replaceChildren(...children); } diff --git a/cigaradvisor/scripts/scripts.js b/cigaradvisor/scripts/scripts.js index 96fb1770..7a146b62 100644 --- a/cigaradvisor/scripts/scripts.js +++ b/cigaradvisor/scripts/scripts.js @@ -93,6 +93,12 @@ 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'; +} + /** * Loads everything needed to get to LCP. * @param {Element} doc The container element