I am trying to override window.close() Method in javascript. Here is my code.
 (function () {
    var _close = window.close;                  
    window.close = function () {
        window.opener.alert("test");
        _close();                             
    };
})();
I am trying to bind this code to new window and execute the inner code when the new window is closed. 
Is is possible to override window.close like this ?
JavaScript does not allow one to close a window opened by the user, using the window. close() method due to security issues. However, we can close a window by using a workaround. The approach to be followed is by opening the current URL using JavaScript so that it could be closed with a script.
The Window. close() method closes the current window, or the window on which it was called. This method can only be called on windows that were opened by a script using the Window.
Try this
You can use window.onbeforeunload event to call any function However, alert,prompt,confirm boxes are not allowed. you can return any string, to show any message.
//var _close = window.onbeforeunload;  
window.onbeforeunload = function () {
   // window.opener.alert("test");
   //_close();        
   return callBack();
};
function callBack()
{
    return "Testing";
}
If you suppose to close any other child window
You can try this
var k = window.open("url");
k.onbeforeunload = callBack;
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With