简体   繁体   English

从打开它的窗口中关闭新窗口

[英]Close new window from window which opened it

I am trying to close a window which have been opened in the following way: 我正在尝试关闭以以下方式打开的窗口:

var myWindow;
function openLink(){
    myWindow = window.open("http://mydir.com", "MsgWindow", "width=600, height=400, left=250, top=200");

    }

This opens a window inside a some kind of popup ( Here you can see how it works ) 这会在某种弹出窗口中打开一个窗口( 在这里您可以看到它的工作方式

What i want to do is when my ajax call finishes (in the opened window) close the window. 我想做的是当我的ajax调用完成时(在打开的窗口中)关闭该窗口。 I am trying with this: 我正在尝试:

 $.ajax ({
  url: "php/copia.php",
  type: "post",
  data: {"params": result,
  "dirDest":dirDest},
  success: function(response) {
        $.mobile.loading('hide');
        window.parent.postMessage('Close popup', '*');//I am trying to send a message to the parent window but it doesnt work
    }

}); 

In the window which opens the new window i have the following: 在打开新窗口的窗口中,我有以下内容:

$( document ).ready(window.addEventListener("message", closePopup, false));

function closePopup(event) {
        myWindow.close();
    }

Solution #1: just call window.close() in success callback. 解决方案1:只需在成功回调中调用window.close()

Solution #2: replace window.parent with window.opener http://www.w3schools.com/jsref/prop_win_opener.asp http://www.w3schools.com/jsref/prop_win_parent.asp 解决方案2:将window.parent替换为window.opener http://www.w3schools.com/jsref/prop_win_opener.asp http://www.w3schools.com/jsref/prop_win_parent.asp

Read more here: When to use window.opener / window.parent / window.top 在此处阅读更多信息: 何时使用window.opener / window.parent / window.top

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

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