微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

内联javascript和iframe的Ajax无法成功调用javascript onLoad

如何解决内联javascript和iframe的Ajax无法成功调用javascript onLoad

我有一个包含 iframe 和 javascript 函数用户控件。 js 函数旨在内联在 html 中。 js 函数名称和逻辑特定于该 iframe UC 实例,因此不会与 iframe 或 iframe UC 的其他实例发生重叠。

我已经采用这种方法来处理整页加载和回发。但是,当我通过更新面板(部分页面加载)使用这种方法时,尽管检查后在 html 中可用,但无法识别该功能

这是我的 iframeUC 代码

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ucIFrame.ascx.cs" Inherits="CPWeb.ucIFrame" %>
<script type="text/javascript">
    function autoResizeIframe<%=ID%>() {
        var newHeight;
        var newWidth;
        var element = document.getElementById('iframe<%=ID%>');
        if (element) {
            newHeight = element.contentwindow.document.body.scrollHeight;
            newWidth = element.contentwindow.document.body.scrollWidth;
        }

        console.log('autoResizeIframe triggered: ' + 'iframe<%=ID%>' + '; resizing to: ' + newWidth + ' x ' + newHeight);
        element.height = (newHeight) + 35 + "px";
        element.width = (newWidth) + 35 + "px";
    }
</script>
<iframe id="iframe<%=ID%>" src="<%=this.IframeURL%>" width="90%" height="500" class="resizable" frameborder="0" onload="autoResizeIframe<%=ID%>()"></iframe>

我的 iframe 在 btn 点击回发时被添加到 phRoutings 中:

<asp:UpdatePanel ID="upRoutings" runat="server">
                <ContentTemplate>
                    <asp:PlaceHolder ID="phRoutings" runat="server"></asp:PlaceHolder>
                </ContentTemplate>
            </asp:UpdatePanel>

我发现唯一有效的解决方案是调用必须在添加任何 iframe 之前在页面上声明的静态 js 函数,但如果 UC 不包含它的特定要求,这似乎是错误的。

Javascript function not working after postback?

这个问题听起来很相似,但是案例涉及一个预先存在的 iframe,并且建议的解决方案最终是一个静态 js 函数,它只是从脚本管理器而不是从内联调用。无论哪种方式都不是理想的解决方案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。