JavaScript open URL in new window/tab | Example code

Use window.open() method to open URL in new window/tab in JavaScript. Don’t forget to add target=”_blank” in the method.

window.open(url, "_blank");

JavaScript open URL in new window Example

HTML Example code:

It will open automatics new tab with URL.

<html>
<body>
	<script>
		var url = new URL("https://www.eyehunts.com");

		window.open(url,"_blank");
	</script>

</body>
</html>

Output:

JavaScript open URL in new window tab

If you want to on click anchor tag:-

<a id="myLink" href="www.google.com" target="_blank">google</a>

Specify a width/height new window tab

window.open(url, windowName, "height=200,width=200");

Do comment if you have any doubts and suggestion on this JS URL example 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.