重构一个使用window.open使用DOM而不是write()的函数 [英] Refactoring a function that uses window.open to use the DOM rather than write()

查看:102
本文介绍了重构一个使用window.open使用DOM而不是write()的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用window.open()生成动态弹出窗口的应用程序。不幸的是,我使用标准的DOM函数(createElement,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创建相同弹出窗口的函数?



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

解决方案

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



  function writePopup(){
var popup = window.open(,_blank,height = 400px,width = 400px);
var doc = popup.document;
doc.title =书面弹出;

var p = doc.createElement('p');
p.innerHTML ='测试写入';
doc.body.appendChild(p);
}


I have an application that uses window.open() to generate dynamic popups. Unfortunately, I've had trouble creating the content of the new windows using the standard DOM functions (createElement, appendChild), and I've gone to using document.write() to generate the page.

Concretely, how can I go from this:

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();
}

To a function that creates the same popup using the DOM?

Edit: I did consider using an absolutely positioned element to simulate a popup, and though it looks better, the users need to be able to print the information being shown.

解决方案

Why not use a library function such as http://plugins.jquery.com/project/modaldialog instead of reinventing the wheel?

[EDIT] OR

   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);
   }

这篇关于重构一个使用window.open使用DOM而不是write()的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆