Onclick Open URL in same window JS | Example Code

There are many ways to open an URL in the same window using HTML or JavaScript. Some methods are:-

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

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

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

Note: use proper http URL to open an website

If you want to do this in HTML:

<a href = "eyehunts.com" target = "_self"> </a>

Onclick Open URL in same window Examples:

HTML example code:

Used window.open for opening link in JavaScript

The second parameter of window.open() is a string representing the name of the target window.

<html>
<body>

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

</body>
</html>

Output:

JavaScript Onclick Open URL in same window

Using HTML anchor Tag

You have to click on the link to open an URL in the same tab.

<html>
<body>
	<a href = "https://eyehunts.com/" target = "_self"> Click Here </a>

</body>
</html>

_self – URL replaces the current page.

Do comment if you have any doubts an suggestion on this JS URL topic.

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.