Skip to content

Onclick Open URL in same window JS | Example Code

  • by

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

Your email address will not be published. Required fields are marked *