From 07b8b33cd5a49c64f711461d917f4fde754b134e Mon Sep 17 00:00:00 2001 From: kailasnadh790 <53608019+kailasnadh790@users.noreply.github.com> Date: Thu, 7 Dec 2023 15:06:04 -0500 Subject: [PATCH] imagecta link fix --- cigaradvisor/blocks/imagecta/imagecta.js | 6 +++++- cigaradvisor/scripts/scripts.js | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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