Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

85
Visualizações
JavaScript to open in a new tab with location.href

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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'); }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda