M. A. Kishawy
M. A. Kishawy

Reputation: 5079

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

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.

Upvotes: 14

Views: 46732

Answers (4)

Random
Random

Reputation: 1

<textarea id="code" placeholder="Put code here!" width="500" height="500">
<DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html></textarea>
<input id="width" placeholder="width">
<input id = "height" placeholder="height">
<button onClick="open();">Open the window!</button>
<button onClick="write();">Apply the html to the window</button>
<button onClick = "resize();">Resize the window!</button>
<script>
var win;
function open(){
win = window.open("", "", "");
win.resizeTo(screen.width, screen.height);
}
function resize(){
win.resizeTo(eval(document.getElementById("width")), eval(document.getElementById("height")))
}
function write(){
win.document.write(document.getElementByid("code").value);
}
</script>

Here you can input things and change the window. Hope you enjoy! :)

Upvotes: 0

Fabien M&#233;nager
Fabien M&#233;nager

Reputation: 140195

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

Upvotes: 24

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91963

function fu() {
  var opened = window.open("");
  opened.document.write("Your HTML here");
}

Upvotes: 2

Roy Tang
Roy Tang

Reputation: 5761

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

Upvotes: 5

Related Questions