Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

82
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda