-1

I am building a custom linktree and Im struggling to find the right command to open these links in a new tab. Currently links are opened in current window as well as in a new tab.

let url = document.createElement('url');
    switch(event.target.innerText) {
      case 'youtube' : url = 'https://www.youtube.com/';break;
      case 'spotify' : url = 'https://open.spotify.com/artist/';break;
      case 'applemusic' : url = 'https://music.apple.com/';break;
      case 'soundcloud' : url = 'https://soundcloud.com/';break;
      case 'amazon' : url = 'https://media.giphy.com/';break;
      default: break;
    }
    if(url) {window.open(top.location.href = url, '_blank');}

When I replace top with window for the last line, like this:

if(url) {window.open(top.location.href = url, '_blank');}

it opens the url in a weird box inside my current window.

On a sidetone this is my first post, so please tell me in case I did something wrong.

8ball
  • 1
  • 1

1 Answers1

0

Your code is changing the URL of the current page - or the topmost ancestor if your page is inside a <frame> or <iframe> - to point to the specified URL, and then also opening a new tab/window for the specified URL.

Just use:

if (url) { window.open(url, '_blank'); }
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151