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

时间:2011-02-19 05:57:04

标签: javascript facebook popupwindow

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 个答案:

答案 0 :(得分:18)

告诉我这是不是你要找的...... 父窗口:

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

答案 1 :(得分: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>

答案 2 :(得分: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>

答案 3 :(得分:0)

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

window.close();