How to open a URL in a new tab in JavaScript

Marika Lam
Sep 15, 2022

Here is how to open a URL in a new tab in JavaScript.

function openInNewTab(url) {
window.open(url, '_blank').focus();
}

//or just
window.open(url, '_blank').focus();

Here is a HTML example of how to call the function above.

<div onclick="openInNewTab('www.test.com');">Something To Click On</div>

Amazon Links to “Must-Haves” as a WFH Software Engineer 🙂💻

24-inch budget-friendly monitor, Portable monitor, Laptop stand, Waterproof desk mat, Keyboard cleaning gel, Six-outlet wall charger and surge protector,, Wi-Fi extender, Blue-light–blocking glasses, Memory foam gel pad, Foam footrest

Courses to learn more about datastructures/algorithms ✍️

Stanford, Duke, UCSD

Courses to learn more about databases 👓

DB Foundations, Data Scientists specialization

Courses to learn more about React 🧢

Full-Stack WebDev, Front-End WebDev with React, IBM Full Stack

--

--