【问题标题】:How to close popup window and redirect the parent window如何关闭弹出窗口并重定向父窗口
【发布时间】:2011-06-30 06:26:20
【问题描述】:
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 在弹出窗口中将用户重定向到我的应用程序时。我需要将代码从弹出窗口发送到父窗口,然后在用户单击允许时关闭弹出窗口。

【问题讨论】:

    标签: javascript facebook popupwindow


    【解决方案1】:

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

    <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="wwww.facebook.com";) 然后它说..“错误:无法获取属性'位置'的值:对象为空或未定义”
    • 你需要 window.opener.location.href 而不是 window.opener.document.location.href
    • 其实window.focus是函数,所以“if (window.focus) {}”总是为真
    【解决方案2】:

    在父页面中编写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>
    

    【讨论】:

      【解决方案3】:

      这是你的父窗口:

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

      【讨论】:

        【解决方案4】:

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

        window.close();

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多