如何让 JavaScript 制作(生成)新页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2004555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:21:23  来源:igfitidea点击:

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

javascripthtmlwebprinting

提问by M. A. Kishawy

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?

我想在页面上制作一个按钮,可以在同一页面中调用JS函数。该函数将需要创建(打开)新窗口,其 HTML 代码是由 JS 函数本身提供的。我怎样才能做到这一点?

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

这样做的目的是从特定页面中生成适合打印的页面。

Please notice: No AJAX can be used.

请注意:不能使用 AJAX。

回答by Fabien Ménager

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

回答by Roy Tang

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

回答by Emil Vikstr?m

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