How to Open URL in Same Window and Tab using JavaScript

In HTML, <a> tag is used to open URL on browser’s window. The href attribute allows to add a hyperlink to the <a> tag and redirect one page to another. By default, the URL is opened in the same window. So, you don’t need to define any attribute in <a> tag to open URL in the same tab or window.

<a href="https://www.codexworld.com">Visit CodexWorld</a>

If you want to open URL in JavaScript, the open() method of the Window interface is the easiest way. The JavaScript window.open() method allows you to open URL in the browser tab or window. You can use _self value in the second parameter of the window.open() method to open the URL in the same tab and in the same window with JavaScript.

The following code snippet opens the URL in the same window and tab of the browser using JavaScript.

window.open("https://www.codexworld.com/", "_self");

1 Comment

  1. Rishi Said...

Leave a reply

keyboard_double_arrow_up