繁体   English   中英

如何关闭弹出窗口并重定向父窗口

[英]How to close popup window and redirect the parent window

string dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" + app_id + "&redirect_uri=" + Server.UrlEncode(my_url) + "&scope=" + permission;
ClientScript.RegisterClientScriptBlock(typeof(Page), "key", "window.open('"+dialog_url+"','_parent','');");

我将此代码用于弹出权限对话框。 当用户单击允许 facebook 在弹出窗口中将用户重定向到我的应用程序时。 我需要将代码从弹出窗口发送到父窗口,然后在用户单击允许时关闭弹出窗口。

告诉我这是否是您要找的...父窗口:

<html>
<head>

    <script language="Javascript">

        function showFBWindow(){
            url = "allowfbchild.html"
            newwindow=window.open(url,'name','height=200,width=150');
            if (window.focus) {newwindow.focus()}
        }

    </script>

</head>
<body>

    <input type="button" OnClick="showFBWindow()" value="Open FB" />

</body>
</html>

子窗口(allowfbchild.html):

<html>
<head>

    <script language="Javascript">

        function redirectToFB(){
            window.opener.location.href="http://wwww.facebook.com";
            self.close();
        }

    </script>

</head>
<body>

    Allow the user to view FB
    <br/>Are you sure?
    <input type="button" value="Ok" OnClick="redirectToFB()" />

</body>
</html>

在父页面中编写如下 javascript

 <script language="Javascript">
     function popitup(url)
     {
      newwindow = window.open(url, "popwin", "width = 320, height = 210,  resizable = no");
      if (window.focus) { newwindow.focus() }
      return false;
      popwin.moveTo(0, 0);
      }
</script>

<a href=""   onclick="return popitup('myapppopup.aspx');return false;">`myapppopup</a>`

然后在弹出窗口中添加到 facebook 的链接,如下所示

<a href="http://wwww.facebook.com"   target="_blank"   onclick="self.close();">facebook </a>

这是您的父窗口:

<script language="javascript">

function open_a_window() 
{
    var w = 200;
        var h = 200;
        var left = Number((screen.width/2)-(w/2));
        var tops = Number((screen.height/2)-(h/2));

        window.open("window_to_close.html", '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+tops+', left='+left);

   return false;
}

// opener:
window.onmessage = function (e) {
  if (e.data === 'location') {
    window.location.replace('https://www.google.com');
  }
};

</script>

<input type="button" onclick="return open_a_window();" value="Open New Window/Tab" />

这是您的子窗口:

<!DOCTYPE html>
<html>
<body onload="quitBox('quit');">

<h1>The window closer:</h1>

<input type="button" onclick="return quitBox('quit');" value="Close This Window/Tab" /> 


<script language="javascript">

function quitBox(cmd) 
{      
    if (cmd=='quit')    
    {   
       window.opener.postMessage('location', '*');
       window.open(location, '_self').close();    
    }     
    return false;   
}

</script>

</body>
</html>

如果要关闭带有链接的弹出窗口,只需使用 click 事件调用以下函数即可。

window.close();

暂无
暂无

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

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