Skip to content

Commit

Permalink
set target
Browse files Browse the repository at this point in the history
  • Loading branch information
nandereck committed Nov 27, 2024
1 parent 27b748c commit d943211
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/analytics/add-global-link-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export function addGlobalLinkHandler(
) {
if (typeof window === 'undefined' || hasHandler) return

const isInternal = (url: string) => {
const hostname = new URL(url).hostname
return hostname === 'hashicorp.com'
}

window.addEventListener('click', (event) => {
const linkElement = (event.target as HTMLElement).closest('a')
const href = linkElement && linkElement.getAttribute('href')
Expand Down Expand Up @@ -76,6 +81,12 @@ export function addGlobalLinkHandler(

callback && callback(url.href)

const isInternalLink = isInternal(url.href)

if (!isInternalLink) {
linkElement.setAttribute('target', '_blank')
}

if (
linkElement.getAttribute('target') === '_blank' ||
event.ctrlKey ||
Expand Down

0 comments on commit d943211

Please sign in to comment.