我正在使用Asp.net/C#环境和Visual 2013创建web应用程序。
我有一个页面(aspx),其中只有一个iframe,还有一个javascript方法,它使用jquery将html放入到该框架中。
<asp:Content runat="server" ID="MainSectionContent" ContentPlaceHolderID="MainSectionContentPlaceHolder">
<section runat="server" id="FrameSection" clientidmode="Static">
<asp:ContentPlaceHolder runat="server" ID="FrameSectionContentPlaceHolder" />
<iframe id="FrameContentFrame" name="FrameContentFrame" src="about:blank" seamless="seamless" onload=" loadFrameContent(); "></iframe>
</section>
<script type="text/javascript">
//<![CDATA[
function loadFrameContent() {
jQuery('#FrameContentFrame').contents().find('html').html('<%= FrameContent %>'); //FrameContent is a c# property which holds a syntactically correct html string
}
//]]>
</script>
</asp:Content>在后面的代码中,我从服务器获取一个长字符串,并将其存储在属性FrameContent中。这个长字符串在语法上是正确的html。
结果应该是: onload,javascript方法运行。iframe由html填充,因此将向用户显示html字符串。
但真正发生的是:什么都没有显示出来。一个Javascript错误会说“函数loadFrameContent是未定义的”。但是如果我在我的网页浏览器中查看页面源的话,这显然是存在的。如果我右键单击该框架所在的空白页面,并选择“重新加载框架”(),该框架将显示Html。
所以: Html字符串是正确的。框架是正确的。该框架可以正确显示html。但是,框架不能显示Html,它需要额外的手动重新加载才能真正显示任何内容!我检查了浏览器上的页面源,Javascript函数出现了,FrameContent属性中填充了正确的html字符串。
发布于 2014-02-04 15:48:34
在我看来,这似乎是一个时间问题。一个空白的iframe将在父页面之前完成加载,因此它将在父页面解析完它的dom之前调用loadFrameContent (因此该方法不会被注册)。去掉iframe装载并添加以下内容:
<script type="text/javascript">
//<![CDATA[
function loadFrameContent() {
jQuery('#FrameContentFrame').contents().find('html').html('<%= FrameContent %>'); //FrameContent is a c# property which holds a syntactically correct html string
}
$(loadFrameContent);
// Or if you're using an older version of jQuery
//$(document).ready(loadFrameContent);
//]]>
</script>https://stackoverflow.com/questions/21556791
复制相似问题