79635924

Date: 2025-05-23 16:52:02
Score: 1.5
Natty:
Report link

After some testing, I found out that the most upvoted answer isn't good for reusability and is ugly, so I ended up going with one of the other suggested solutions and created this method in my utils:

openInNewTab(url: string): void {
  if (this.isSafari()) {
    const a = document.createElement('a')
    a.setAttribute('href', url)
    a.setAttribute('target', '_blank')
    setTimeout(() => a.click())
  } else {
    this.window.open(url, '_blank')
  }
}

For those curious how I determine Safari:

// https://stackoverflow.com/a/70585394/20051807
isSafari(): boolean {
  return (this.window as any).GestureEvent || this.window.navigator.userAgent.match(/iP(ad|od|hone)/i)
}

Tested on iPhone and Mac.

Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Blacklisted phrase (0.5): upvote
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Ilya Konrad