使用 javascript 将 html 写入新窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15070023/
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
Writing html to a new window with javascript
提问by Nick
I have been doing some research on opening a new window and writting HTML to it with jQuery/JavaScript and it seems like the proper way to do it is to:
我一直在做一些关于打开一个新窗口并使用 jQuery/JavaScript 向其写入 HTML 的研究,似乎正确的方法是:
Create a variable for the new window
为新窗口创建一个变量
var w = window.open();
Insert the new data and work the variable
插入新数据并处理变量
$(w.document.body).html(data);
And to me, that makes complete sense. however when i try to incorporate this into my script ("data" being the holder for the HTML) it does not open a new window... unless I'm just missing something simple which as far as I can tell it looks great...
对我来说,这是完全有道理的。但是,当我尝试将其合并到我的脚本中(“数据”是 HTML 的持有者)时,它不会打开一个新窗口......除非我只是遗漏了一些简单的东西,据我所知它看起来很棒。 ..
function newmore(str) {
var identifier = 4;
//get the history
$.post("ajaxQuery.php", {
identifier : identifier,
vanid : str
},
//ajax query
function(data) {
//response is here
var w = window.open();
$(w.document.body).html(data);
});//end ajax
}
Any ideas?
有任何想法吗?
P.S. there seems to be no errors in the console
PS控制台中似乎没有错误
回答by jfriend00
Your new window is probably being blocked by the popup blocker built into most browsers. If you create the new window as a direct result of a user action (key, click), then the browser usually will not block it. But, if you wait until sometime later (like after an ajax call completes), then it will get blocked and that is probably what is happening in your case.
您的新窗口可能被大多数浏览器内置的弹出窗口阻止程序阻止了。如果作为用户操作(键、单击)的直接结果创建新窗口,则浏览器通常不会阻止它。但是,如果您等到稍后(例如在 ajax 调用完成之后),那么它就会被阻塞,这可能就是您的情况。
So, the fix is usually to create the window immediately in direct response to the user event (don't wait until the ajax call completes), keep the window handle in a variable and then put the content in the window later after your ajax call completes.
因此,修复方法通常是立即创建窗口以直接响应用户事件(不要等到 ajax 调用完成),将窗口句柄保存在变量中,然后在 ajax 调用后将内容放入窗口中完成。
function newmore(str){
var identifier = 4;
// create window immediately so it won't be blocked by popup blocker
var w = window.open();
//get the history
$.post("ajaxQuery.php", {
identifier : identifier,
vanid : str
},
//ajax query
function(data) {
//response is here
$(w.document.body).html(data);
});//end ajax
}
回答by Pointy
Try instead:
试试吧:
var w = window.open();
w.document.write(data);
The "innerHTML" property of the document object (which is what jQuery's .html()
uses) represents the HTML document, which a new window doesn't have. Even if it did, putting a complete document insidean HTML document doesn't really make sense. It's a freshly-created document, so you can just write to it.
文档对象的“innerHTML”属性(这是 jQuery.html()
使用的)表示 HTML 文档,新窗口没有。即使是这样,将一个完整的文档放入HTML 文档也没有什么意义。这是一个新创建的文档,因此您只需写入即可。
回答by thitemple
This peace of code will work:
这种和平的代码将起作用:
var data = "<h1>Test</h1>";
var w = window.open("", "mywindow1", "width=350,height=150");
$(w.document.body).html(data);
You have to inform some parameters when opening new windows.
打开新窗口时,您必须通知一些参数。
But, if possible, I'd hardly recommend that you use another way like, jquery UI or Twitter Bootstrap for doing that, so you will not be using pop-ups.
但是,如果可能的话,我几乎不建议您使用另一种方式,例如 jquery UI 或 Twitter Bootstrap,这样您就不会使用弹出窗口。