Open window in JavaScript with HTML inserted

How would I open a new window in JavaScript and insert HTML data instead of just linking to an HTML file?



I would not recomend you to use document.write as others suggest, because if you will open such window twice your HTML will be duplicated 2 times (or more).

Use innerHTML instead

var win ="", "Title", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=200,top="+(screen.height-400)+",left="+(screen.width-840));
win.document.body.innerHTML = "HTML";

When you create a new window using open, it returns a reference to the new window, you can use that reference to write to the newly opened window via its document object.

Here is an example:

var newWin = open('url','windowName','height=300,width=300');
newWin.document.write('html to write...');

You can open a new popup window by following code:

var myWindow ="", "newWindow", "width=500,height=700");

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

What I will recommend is that first you create a new html file with any name. In this example I am using


And make sure to add all content in that file such as bootstrap cdn or jquery, means all the links and scripts. Then make a div with some id or use your body and give that a id. in this example I have given id="mainBody" to my newFile.html <body> tag

<body id="mainBody">

Then open this file using

var myWindow ="newFile.html", "newWindow", "width=500,height=700");

And add whatever you want to add in your body tag. using following code

 var myWindow ="newFile.html","newWindow","width=500,height=700");  

   myWindow.onload = function(){
     let content = "<button class='btn btn-primary' onclick='window.print();'>Confirm</button>";
   myWindow.document.getElementById('mainBody').innerHTML = content;


it is as simple as that.


You can use to open a new window/tab(according to browser setting) in javascript.

By using document.write you can write HTML content to the opened window.


You can also create an "example.html" page which has your desired html and give that page's url as parameter to

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

Here's how to do it with an HTML Blob, so that you have control over the entire HTML document:

This is the code, but StackOverflow blocks the window from being opened (see the codepen example instead):

const winHtml = `<!DOCTYPE html>
            <title>Window with Blob</title>
            <h1>Hello from the new window!</h1>

const winUrl = URL.createObjectURL(
    new Blob([winHtml], { type: "text/html" })

const win =


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.