首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Iframe没有显示onload,但它在手动重新加载之后显示。

问Iframe没有显示onload,但它在手动重新加载之后显示。
EN

Stack Overflow用户
提问于 2014-02-04 15:42:19
回答 1查看 907关注 0票数 0

我正在使用Asp.net/C#环境和Visual 2013创建web应用程序。

我有一个页面(aspx),其中只有一个iframe,还有一个javascript方法,它使用jquery将html放入到该框架中。

代码语言:javascript
复制
<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字符串。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-04 15:48:34

在我看来,这似乎是一个时间问题。一个空白的iframe将在父页面之前完成加载,因此它将在父页面解析完它的dom之前调用loadFrameContent (因此该方法不会被注册)。去掉iframe装载并添加以下内容:

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21556791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档