How to usewindow.location.href Property in Javascript
Use the window.location.href property to open a URL in the same window and in the same tab.
Syntax:
window.location.href = url;
Parameters:
The “url” is the URL that you want to load in the same window and in the same tab.
Example: In this example, we have used the window.location.href property to open the “w3wiki.org” URL in the same window and in the same tab. When the button is clicked, the SameTab() function is called, which sets the window.location.href property to the desired URL, loading it in the same window and in the same tab.
HTML
<!DOCTYPE html> < html > < head > < title > Open URL in same window and in same tab </ title > < style > body { text-align: center; } </ style > </ head > < body > < p > Click the button to open < br /> < b style = "color:green; font-size:2rem" > w3wiki.org </ b > < br /> in same window & same tab < br /> </ p > < button onclick = "SameTab()" > Open using window.location.href </ button > < script > function SameTab() { window.location.href = "https://www.w3wiki.org"; } </ script > </ body > </ html > |
Output:
How to open URL in same window and same tab using JavaScript ?
Opening a URL in the same window and in the same tab is a common requirement for web developers. This functionality is useful when you want to load a new web page or document into the current browser window and replace the content of the current web page.
There are several ways to open URLs in the same window and in the same tab using JavaScript:
Table of Content
- Using window.location.replace() Method
- Using window.location.href Property
- Using window.open() Method