代码之家  ›  专栏  ›  技术社区  ›  technomalogical

打开一个新的浏览器窗口/iframe并在TEXTAREA中从HTML创建新文档?

  •  3
  • technomalogical  · 技术社区  · 14 年前

    我正在尝试使用HTML5的新离线功能编写一个web应用程序。在这个应用程序中,我希望能够编辑一些HTML—一个完整的文档,而不是文档中的一个片段 <textarea> <iframe> ,尚未决定)的HTML < . 除了本地客户机之外,新内容不会持久化到任何地方,因此在 window.open src 上的属性 <iframe>

    Putting HTML from the current page into a new window “,这让我有了一段路。这种技术似乎可以很好地处理片段,但是我没有成功地加载一个全新的HTML文档。奇怪的是,当我在Firebug中查看DOM时,我看到了它无法呈现的新HTML。

    是否可以在新的窗口或窗口中呈现生成的HTML文档 <iframe> ?

    :下面是一个“工作”示例,说明我是如何尝试实现这一点的:

    <!doctype html>
    <html>
    <head>
    <title>Test new DOM</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script>
        function runonload() {
            return $("#newcode")[0].value;
        }
        $(function() {
            $("#runit").click(function() {
                w=window.open("");
                $(w.document).ready(function() {
                    $(w.document).html(w.opener.runonload());
                });
            });
        });
    </script>
    </head>
    <body>
    <textarea id="newcode">
    &lt;!doctype html&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;New Page Test&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h1&gt;Testing 1 2 3&lt;/h1&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    </textarea>
    <br/>
    <button id="runit">Run it!</button>
    </body>
    </html>
    
    2 回复  |  直到 8 年前
        1
  •  4
  •   bobince    14 年前
    $(w.document).html(w.opener.runonload());
    

    你不能设置 innerHTML 或者,因此,jQuery的 html() 在文档对象本身上。

    即使你可以,你也不能用 html() ,因为它在元素的上下文中解析给定的标记(通常 <div> )从 现在的 <html> / <body> /等内部 <部门>

    在这种情况下,传统的方式仍然是最好的:

    w= window.open('', '_blank');
    w.document.write($('#newcode').val());
    w.document.close();
    

    当你 可以 注入 变成一个弹出窗口 document.documentElement ,如果你这样做,你就没有机会设定 <!DOCTYPE>

        2
  •  5
  •   Darren Westall    14 年前

    试试这个:

    <SCRIPT LANGUAGE="JavaScript">
    function displayHTML(form) {
    var inf = form.htmlArea.value;
    win = window.open(", ", 'popup', 'toolbar = no, status = no'); win.document.write("" + inf + ""); } // </script>
    <form>
    <textarea name="htmlArea" cols=60 rows=12> </textarea> <br> <input type="button" value=" Preview HTML (New Window)" onclick="displayHTML(this.form)"> </form>