将页面从Iframe更改为主窗口

时间:2014-01-19 21:14:01

标签: javascript asp.net .net vb.net iframe

我在iframe内部工作,当我完成工作时,我想在主窗口中更改页面(不在iframe内)但在iframe的一侧。 为此,我使用以下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

我在iFrame

中的页面中使用以下脚本
<script type="text/javascript">
     function ChangePage(startPage, sendPage, message) {
         var sPage = document.getElementById(startPage);
         sPage.contentWindow.postMessage(message, sendPage);
     }
    </script>

我还在我想要更改的页面(父页面)中使用以下脚本:

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

我从上面得到的是拥有所需的页面(示例中的消息)但是在框架内部。
Help01.aspx页面中的脚本执行得很好。但是在这个窗口中,而不是在父窗口中(Assistance,aspx)
结果我无法离开iFrame(在桌子里面) 有人知道该怎么办吗?

1 个答案:

答案 0 :(得分:1)

我认为你需要在这里使用像postMessage这样的东西。这取决于您需要支持的浏览器,但支持非常好。这将让你在主窗口和iFrame之间“交谈”。

该链接将引导您完成如何设置,但其要点是您需要将事件侦听器附加到主窗口,然后将消息从iFrame发布到外部窗口。然后在消息进入时在外部窗口中进行重定向。

所以你有Dim msg As String...的地方,你会希望它成为一些将消息发布到外框的代码。像window.postMessage(..)

这样的东西