2

我有一个window.open()用于生成动态弹出窗口的应用程序。createElement不幸的是,我在使用标准 DOM 函数( , appendChild)创建新窗口的内容时遇到了麻烦,我已经使用document.write()它来生成页面。

具体来说,我该怎么做:

function writePopup()
{
    var popup = window.open("", "popup", "height=400px, width=400px");
    var doc = popup.document;
    doc.write("<html>");
    doc.write("<head>");
    doc.write("<title>Written Popup</title>");
    doc.write("</head>");
    doc.write("<body>");
    doc.write("<p>Testing Write</p>");
    doc.write("</body>");
    doc.write("</html>");
    doc.close();
}

对于使用 DOM 创建相同弹出窗口的函数?

编辑:我确实考虑过使用绝对定位的元素来模拟弹出窗口,虽然它看起来更好,但用户需要能够打印显示的信息。

4

6 回答 6

2

Just doing quick tests, I can get doc to append DOM created HTML to my popup like so:

var popup = window.open("", "popup", "height=400px, width=400px"); 
var doc = popup.document.documentElement;
var p = document.createElement("p"); 
p.innerHTML = "blah"; 
doc.appendChild(p);

My example produces totally invalid HTML I know, but it works (with limited testing obviously).

于 2008-11-05T15:38:54.523 回答
2

One dump of .innerHTML should perform better than a zillion lines of document.write();

I would build up the DOM as needed and dump in using

doc.innerHTML = newDOM;

Still kind of hacky, but better than document.write();

于 2008-11-05T15:45:41.017 回答
2

I've gone to using document.write() to generate the page.

I think you will probably have to use at least some document.write(), to put a skeleton page in place before you can interact with the DOM:

doc.write('<!DOCTYPE ...><html><head></head><body></body></html>');
doc.close();
doc.body.appendChild(...);

Before write() has been called, a new window's document is in an indeterminate state, there is no document yet to interact with. Firefox at least puts a temporary skeleton document in until you first call document.write(), but I can't see that this is actually documented anywhere so it's probably best not relied upon.

var popup = window.open("", "popup", "height=400px, width=400px");

No ‘px’ unit needed in window.open.

于 2009-02-12T14:24:12.613 回答
1

为什么不使用诸如 http://plugins.jquery.com/project/modaldialog之类的库函数,而不是重新发明轮子呢?

[编辑] 或

function writePopup(){
    var popup = window.open("", "_blank", "height=400px, width=400px");
    var doc = popup.document;
    doc.title = 'Written Popup';

    var p = doc.createElement('p');
    p.innerHTML = 'Testing Write';
    doc.body.appendChild(p);
}
于 2008-11-05T15:07:34.040 回答
0

Would it be possible to have another page that generates the dynamic content, and pop it open, passing arguments that identify the content you want generated, instead of trying to write to a new window?

于 2008-11-05T15:08:56.050 回答
0

you could use something like

function writePopup(){
  var htmlString=[
    "<html>",
    "<head>",
    "<title>Written popup</title>",
    "</head><body>",
    "<p>Testing write</p>",
    "</body></html>"
  ].join("\n");
  window.open("data:text/html,"+encodeURIComponent(htmlString), "_blank", "height=400px, width=400px");
}
于 2010-10-25T05:49:31.237 回答