简体   繁体   English

将页面从iframe更改为主窗口

[英]Change page from an Iframe to the Main Window

I'm working inside an iframe and when i finish my job I want to change page (NOT inside iframe) but out side of this iframe, in the main window. 我正在iframe内工作,当我完成工作时,我想在主窗口中更改页面(不在iframe内),但要在此iframe的外面。
For this I use the following Sub: 为此,我使用以下Sub:

Public Sub ChangePage(sender As Object, e As EventArgs)
        Dim myNewAsp As New AspNetSqlProvider
        Dim dbHandler As New DBHandler
        Dim srvHandler As New ServerHandler
        Dim aspProvider As New MyAspNetSqlMembershipProvider
        Dim errAnswer As New Register
        Dim assit As New Assistance
        Dim currentUser As MembershipUser = Nothing
        Dim message As String = Nothing
        Dim msg As String = Nothing
        Try
            Dim url As String = "/Pages/Account/Login.aspx"
            Dim postWindow As Object   ' "/Pages/Account/Assistance.aspx"
            Dim startWindow As Object   ' "/Pages/Support/Asp/Help01.aspx"
            Dim urlURI As String = HttpContext.Current.Request.Url.AbsoluteUri
            Dim urlPath As String = HttpContext.Current.Request.Url.AbsolutePath
            Dim myServerName As String = Strings.Left(urlURI, urlURI.Length - urlPath.Length)
            url = myServerName + url
            startWindow = Me.GetType().FullName
            postWindow = Assistance.PageUrlType
            msg = "Hellow World"
            'msg = "<script type='text/javascript'> $(document).ready(function(){  window.location.href='" + url + "';  }); </script>"
            Dim script As Type = Me.Page.GetType()  '  Assistance.PageType.BaseType 
            Dim postmsg As String = "ChangePage('" & startWindow & "', '" & postWindow & "', '" & msg & "')"
            If Not Page.ClientScript.IsStartupScriptRegistered(Me.GetType(), "sendMessage") Then
                Page.ClientScript.RegisterStartupScript(script, "sendMessage", postmsg)
            End If

        Catch eq As SqlException
            srvHandler.ErrorAnswer = DBHandler.GetError(1, Nothing, Nothing, eq, Nothing)
            srvHandler.ErrorAnswer = srvHandler.ErrorAnswer & "\r\n Asp.SQLProvider.ResetPassword "
            ServerHandler._InnerError += srvHandler.ErrorAnswer
            Register.errorCall = True
            Return
        Catch Ex As Exception
            srvHandler.ErrorAnswer = DBHandler.GetError(3, Ex, Nothing, Nothing, Nothing)
            srvHandler.ErrorAnswer = srvHandler.ErrorAnswer & "\r\n Asp.SQLProvider.ResetPassword"
            ServerHandler._InnerError += srvHandler.ErrorAnswer
            Register.errorCall = True
            Return

        End Try
    End Sub

I use the following script in the Page inside the iFrame : 我在iFrame内的Page中使用以下脚本:

<script type="text/javascript">
     function ChangePage(startPage, sendPage, message) {
         var sPage = document.getElementById(startPage);
         sPage.contentWindow.postMessage(message, sendPage);
     }
    </script>

I also use the following script in the Page I want to change (parent Page): 我还在要更改的页面(父页面)中使用以下脚本:

<script type="text/javascript">
    window.addEventListener("message", receiveMessage, false);
    function receiveMessage(event) {
        event.origin !== message;
        event.source.postMessage("'" + message + "'", event.origin);
            return;
    }

what I got from the above is to have the desired page (the message in the example) BUT inside of the frame. 我从上面得到的是在框架内放置所需的页面(示例中的消息)。
The scrip in the Help01.aspx page is executed just fine. 可以很好地执行Help01.aspx页中的脚本。 But in this window, and not in the parent one (Assistance,aspx) 但是在此窗口中,而不是在父窗口中(Assistance,aspx)
As a result I can't get out of the iFrame (which is inside of a table). 结果,我无法离开iFrame(位于表内部)。
Is someone to know what to do about it? 有人知道该怎么做吗?

I think you'll need to use something like postMessage here. 我认为您需要在此处使用诸如postMessage之类的东西。 It depends on the browsers you need to support, but support is pretty good. 这取决于您需要支持的浏览器,但是支持非常好。 That will let you "talk" between the main window and the iFrame. 这样一来,您就可以在主窗口和iFrame之间“对话”。

That link will walk you through how to set it up, but the gist of it is that you'll need to attach an event listener to the main window and then post a message from the iFrame to the outer window. 该链接将引导您完成设置过程,但要点是,您需要将事件侦听器附加到主窗口,然后将消息从iFrame发布到外部窗口。 Then do your redirect in the outer window when the message comes in. 收到消息后,请在外部窗口中进行重定向。

So where you have Dim msg As String... you'll want that to be some code that posts a message to the outer frame. 因此,如果您有Dim msg As String...您将希望它是一些将消息发布到外部框架的代码。 Something like window.postMessage(..) 类似于window.postMessage(..)

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

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