Skip to content

JavaScript open URL in the same window/tab | Example

  • by

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

Simple HTML example code.

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 or suggestions 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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading