
Open a URL in a new tab in Javascript
JavaScript can be used to open a new window. The window.open() method allows you to open a new browser window without leaving the current page. It is useful when you need to show a popup advertisement or instructions without leaving the current window.
JavaScript Code:
Open a URL in a new tab.html
<!DOCTYPE html>
<html>
<head>
<title>Open an URL in new tab using JavaScript</title>
<link rel="stylesheet" href="newtab.css">
</head>
<body style="margin-top: 27%; margin-left: 45%;">
<button class="btn" id="clickBtn">Click Here To open New Tab</button>
<script type="text/javascript">
const button = document.querySelector('#clickBtn');
// add click event listener
button.addEventListener('dblclick', () => {
// open a URL in new tab
window.open('file:///D:/JS%20by%20Sailaja/Confident/Day-5/3%20%20refresh%20a%20page%20in%20Javascript.html', '_blank');
});
</script>
</body>
</<html>newtab.css
*{
margin: 0px;
padding:0px;
}
body{
background-image: url('refresh.jpg');
background-repeat: no-repeat;
width:100%;
background-size: cover;
}
.btn{
background-color:rgb(10, 245, 10);
color: aliceblue;
padding: 5px;
margin-left: 43%;
margin-top: 18px;
}Output:

Share: