14

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
  • 5,001
  • 11
  • 47
  • 72

4 Answers4

23
var opened = window.open("");
opened.document.write("<html><head><title>MyTitle</title></head><body>test</body></html>");
M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
Fabien Ménager
  • 140,109
  • 3
  • 41
  • 60
4
var w = window.open("");
w.document.writeln("<the html you wanted to write>")
Roy Tang
  • 5,643
  • 9
  • 44
  • 74
2
function fu() {
  var opened = window.open("");
  opened.document.write("Your HTML here");
}
Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
0
<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! :)

Random
  • 1
  • 2