avaScript is a programming language used to make websites more responsive, interactive, and user-friendly. It can access and modify an HTML web page's content, define the set of instructions that the web page should follow, and react to events triggered by the browser or user.
To open a URL in a new tab/window in JavaScript, we use the window.open(URL, target) method. To open a new tab, however, we must use _blank as the second parameter of this method.
To open a URL in a new tab/window in JavaScript, we use the window.open(URL, target) method. To open a new tab, however, we must use _blank as the second parameter of this method.
Note: Avoid adding a third parameter because the link will be redirected to a new window rather than a new tab.
Code example
Code:
<!DOCTYPE html>
<html>
<head>
<title>Educative</title>
</head>
<body>
<button onclick="openTab()">Educative</button>
<script>
function openTab() {
window.open("https://www.educative.io/", "_blank");
}
</script>
</body>
</html>