Sourav Das Sourav Das - 25 days ago 12
HTML Question

Closing a Parent Window after Modal is closed in JQuery

I am trying to close the Parent Window when a Modal Window is Closed.
I created a custom Modal Window using JQuery UI and i am not able to close the parent window.

$(document).ready(function () {
dialog = $("#dialog").dialog({
autoOpen: false,
height: 300,
width: 500,
modal: true,
buttons: {
"Approve": addUser,
Cancel: function () {
dialog.dialog("close");
}
}
});

});

function addUser() {

var getJSON = function (url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.withCredentials = true;
xhr.onload = function () {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
};

getJSON(url).then(function (data) {
var result = "Approved Successfully"
dialog.dialog("close");
alert(result);
window.close(); ///Here, the PARENT Window is not closing

}, function (status) { //error detection....
alert('Something went wrong.');
});
}


Here, my Modal Box is closing but window.close() is not closing the parent window.

I am getting an Message "Scripts may close only the windows that were opened by it."

How to solve this issue.

Thanks

Answer

Create function in parent window

window.parent.close();
Comments