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

77
Views
React Redirect in New Tab Programmatically

I'm trying to open a new tab with a given URL programmatically in React.

const navigate = (href) => {
    try {
        $(`<a href="${href}" target="_blank"></a>`)[0].click();
    } catch (ex) {
        console.log(ex);
        window.open(href, `_newtab${Math.floor(Math.random() * 999999)}`) ||
            window.location.replace(href);
    }
};

The jQuery solution works best, however it doesn't always work - for example in Safari on iPhones, this does nothing.

The

window.open(href, `_newtab${Math.floor(Math.random() * 999999)}`) ||
            window.location.replace(href);

is less effective as sometimes it opens a popup, and sometimes it opens the new link in the same tab as my application. However, it works on all devices.

What I would like to do is try the jQuery solution first, and if this doesn't do anything, trigger the second option as backup.

However, I noticed that when

$(`<a href="${href}" target="_blank"></a>`)[0].click();

fails on iPhone, it doesn't actually trigger an exception, so the catch block never executes.

Is there another way I can approach this?

I looked at this question to create the above function: javascript window.location in new tab

about 4 years ago · Juan Pablo Isaza
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!