Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

81
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!