简体   繁体   English

console.log(this.contentWindow.location) 在 JQuery 脚本中不起作用

[英]console.log(this.contentWindow.location) not working in JQuery script

I am trying to achieve something with modal pop up.我正在尝试通过模式弹出窗口来实现某些目标。

I have a modal pop up in page A in my site which will open page B on the modal pop up, and inside the same modal pop up, page B will perform a task and then redirect to page C which will contain in the same modal pop up.我的网站页面 A 中有一个模式弹出窗口,它将在模式弹出窗口中打开页面 B,在同一模式弹出窗口中,页面 B 将执行一项任务,然后重定向到页面 C,该页面将包含在同一模式中弹出。 Here I want to disable the "Close button" in the modal pop up when page B is active, and enable it when page C redirected.在这里,我想在页面 B 处于活动状态时禁用模式弹出窗口中的“关闭按钮”,并在页面 C 重定向时启用它。

below is the Jquery code I used, when I run the code, the pageB loads, the button hides, then when pageC loads, but the button will not show.下面是我使用的 Jquery 代码,当我运行代码时,pageB 加载,按钮隐藏,然后当 pageC 加载时,按钮不会显示。

NB: my page use master page, and I have Scriptmanager in masterpage注意:我的页面使用母版页,母版页中有 Scriptmanager

Please who can find the error in this code.请谁能找到这段代码中的错误。

<script src="Scripts/jquery-3.4.1.min.js"></script>   
    <script src="Scripts/MicrosoftAjax.js"></script>

   <script type="text/javascript">
       function displayFrame()
       {
           $("#<%=Irm1.ClientID%>").attr("src", "pageB.aspx");
           $("[id*='Button2']").hide();
           $("#<%=Irm1.ClientID%>").on("load", function () {
               console.log(this.contentWindow.location);
               if (this.contentWindow.location.pathname == "/pageC.aspx") {
                   $("[id*='Button2']").show();
               }
           });
       }
   </script>

<asp:Button ID="btpageA" runat="server" OnClientClick="displayFrame()" Text="show page B" Width="99px" BackColor="#6699FF" Font-Bold="True" ForeColor="White" Height="38px" />

     <cc1:ModalPopupExtender ID="mp1" runat="server" PopupControlID="Panl1" TargetControlID="btpageA"  
    CancelControlID="Button2" BackgroundCssClass="Background">  
</cc1:ModalPopupExtender>  
<asp:Panel ID="Panl1" runat="server" CssClass="Popup" align="center" style = "display:none">  
    <iframe style=" width: 370px; height: 300px;" id="Irm1" runat="server"></iframe>  
   <br/>  
    <asp:Button ID="Button2" runat="server" Text="Close" />  
</asp:Panel>

Place the onload event before changing the src:在更改 src 之前放置 onload 事件:

function displayFrame() {
    $("#<%=Irm1.ClientID%>").on("load", function () {
        console.log(this.contentWindow.location);
        if (this.contentWindow.location.pathname == "/pageC.aspx") {
            $("[id*='Button2']").show();
        }
    });
    $("#<%=Irm1.ClientID%>").attr("src", "pageB.aspx");
    $("[id*='Button2']").hide();
}

And if it still doesn't work check if is there any error in the javascript console.如果它仍然不起作用,请检查 javascript 控制台中是否有任何错误。 If the iframe content is from another domain web browsers don't let you access to its properties.如果 iframe 内容来自另一个域 web 浏览器不允许您访问其属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM