How can I make JavaScript make (produce) new page?

34,802

Solution 1

var opened = window.open("");
opened.document.write("<html><head><title>MyTitle</title></head><body>test</body></html>");

Solution 2

var w = window.open("");
w.document.writeln("<the html you wanted to write>")

Solution 3

function fu() {
  var opened = window.open("");
  opened.document.write("Your HTML here");
}
Share:
34,802
M. A. Kishawy
Author by

M. A. Kishawy

Learning is my Pleasure!

Updated on April 22, 2020

Comments

  • M. A. Kishawy
    M. A. Kishawy about 4 years

    I would like to make a button on a page that can call a JS function in the same page. The function will need to create (open) new window which its HTML code was given from the JS function itself. How can I do that?

    The purpose of this is to produce a print friendly page out of a specific page.

    Please notice: No AJAX can be used.

  • M. A. Kishawy
    M. A. Kishawy over 14 years
    it opens new window however it doesn't write anything in it!!
  • Emil Vikström
    Emil Vikström over 14 years
    It works for me in Firefox. Which browser are you trying this in, and do you get any errors?
  • M. A. Kishawy
    M. A. Kishawy over 14 years
    No errors! but it looks like it won't show the text you write unless you use the HTML properties as Fabien wrote. Thanks