JavaScript open URL in the same window/tab | Example

There are many ways to open an URL in the same window using JavaScript.

/* FIRST WAY*/
window.open("eyehunts.com", "_self")

/* SECOND WAY*/
window.location.href = "eyehunts.com"

/* THEARD WAY*/
window.location.replace("eyehunts.com")

JavaScript open URL in the same window

window.open

<html>
<body>

	<script>
		window.open("https://eyehunts.com/", "_self")
	</script>

</body>
</html>

location.href

<html>
<body>

	<script>
		window.location.href = "https://eyehunts.com/";
	</script>

</body>
</html>

location.replace

<html>
<body>

	<script>
		window.location.replace("eyehunts.com");
	</script>

</body>
</html>

Output:

JavaScript open URL in the same window/tab

Do comment if you have any doubts and suggestion on this JS URL code.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.