Skip to content

JavaScript open new tab with content | Example Code

Use winPrint.document.write method to pass the content to new tab in JavaScript.

Open HTML in a new tab or window code:-

var tab = window.open('about:blank', '_blank');
tab.document.write(html); // where 'html' is a variable containing your HTML
tab.document.close(); // to finish loading the page

JavaScript open a new tab with content

HTML example code:

Create a new window using the open method, it returns a reference to the new window, uses that reference to write to the newly opened window via its document object.

<html>
<body>
	<script>
		var winPrint = window.open('', '', width=800,height=600,toolbar=0);

		winPrint.document.write('<title>Print Report</title> Hello World');	
	</script>

</body>
</html>

Output:

JavaScript open new tab with content

Afterward, you can add HTML using both myWindow.document.write(); or myWindow.document.body.innerHTML = "HTML";

URL as parameter to window.open

var url = '/example.html';
var myWindow = window.open(url, "", "width=800,height=600");

Do comment if you have any doubts and suggestions on this JS 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

Discover more from Tutorial

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

Continue reading