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

9
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时,会在弹出窗口中将用户重定向到我的应用程序。我需要将弹出窗口中的代码发送到父窗口,然后在用户点击允许时关闭弹出窗口。

4个回答

20

告诉我这是否是你正在寻找的内容... 父窗口:

<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>

是的,但当我打开弹出窗口时,页面会重定向到mypage.html。我在此页面上使用代码("window.opener.location.href="http://wwww.facebook.com";)然后它显示..“错误:无法获取属性'location'的值:对象为null或未定义” - JBJumboJB
1
你需要使用 window.opener.document.location.href 而不是 window.opener.location.href - cprogcr
实际上,window.focus是一个函数,因此“if (window.focus) {}”将始终为真。 - Bohdan Lyzanets

0
在父页面中写入以下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>

0
如果您想通过链接关闭弹出窗口,只需使用点击事件调用以下函数即可。

window.close();


0

这是您的父窗口:

<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接