How to Open URL in Same Window and Tab using JavaScript
In this tutorial, We have shared with you How to Open URLs in the Same Window and Tab using JavaScript.In HTML, the anchor tag is used to open URLs in the browser window. The
href
attribute is used to add a hyperlink to the tag and redirect one page to another. By default, the URL opens in the same window. You don’t need to set any attributes in the tag to open the URL in the same tab or window.If you want to do this in HTML:
1 2 3 4 5 6 |
<html> <body> <p>Click the link to open </p> <a href="https://techarise.com">Open Tech Arise</a> </body> </html> |
If you want to open the URL in JavaScript, use the
window.open()
method of Window. The window.open()
JavaScript method lets you open the URL in the browser window or tab. You can use the _self value in the second parameter of the window.open()
method to open the URL in the same tab and window with JavaScript.Let’s try out the following example to understand how to method work with JavaScript:
1 2 |
// JavaScript Code Here window.open("https://techarise.com/", "_self"); |