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

时间:2014-05-30 09:33:53

标签: javascript jquery html ajax

我试图关闭一个以下列方式打开的窗口:

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

    }

这会在某种弹出窗口(Here you can see how it works

中打开一个窗口

我想要做的是当我的ajax调用完成时(在打开的窗口中)关闭窗口。我正在尝试这个:

 $.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
    }

}); 

在打开新窗口的窗口中,我有以下内容:

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

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

1 个答案:

答案 0 :(得分:0)

解决方案#1:只需在成功回调中调用window.close()

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

在此处阅读更多内容:When to use window.opener / window.parent / window.top