如何将大型HTML文件注入iFrame?

时间:2011-06-29 15:23:09

标签: javascript html asp.net-mvc

我需要动态地将HTML注入iFrame。如何“包装”这个html以便它被正确转义,以便我可以将其作为js var传递?

这就是我所拥有的。到目前为止,看起来html的第一行存储在'content'中,但其余部分显示在浏览器窗口中(框架外)。

    <script type="text/javascript" language="javascript">
     var content = <%=Model.Html%>;
    var doc = document.getElementById("testFrame");
    if (iframe.contentDocument)
        doc = iframe.contentDocument; // For NS6
    else if (iframe.contentWindow)
        doc = iframe.contentWindow.document; // For IE5.5 and IE6
    // Put the content in the iframe
    doc.open();
    doc.writeln(content);
    doc.close();
</script>  

我在页面上有一个带有

的iFrame
  

id =“testFrame”

     

&LT;%= Model.Html%GT;包含我想要注入的HTML。它包括一些JS内联。这个文件是我需要“包装”的。

2 个答案:

答案 0 :(得分:0)

您是否考虑过使用iframe的.src属性?

var doc = document.getElementById("testFrame");
doc.src = <%=Model.html%>;

或者您也可以使用AJAX刷新它并使用doc.src=req.responseText;

答案 1 :(得分:0)

由于这是一个ASP.NET MVC应用程序,我刚刚创建了一个返回Html的新动作(GetHtml)。

我将iFrame的src设置为这个新动作。像这样:

<iframe id="testFrame" name="testFrame" src ="<%= Url.Action("GetHtml","MyController", new{href = Model.Href}) %>>" width="100%" height="600">
    <p>
        Your browser does not support iframes.
    </p>
</iframe>

控制器:

public ActionResult GetHtml()
{
...
return Content(htmlContent, "text/html");
}
相关问题