Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding window.close in Javascript

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 ?

like image 649
Rajesh Dhiman Avatar asked Aug 22 '13 12:08

Rajesh Dhiman


People also ask

Can JavaScript close a window?

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.

What is the method for close window in JavaScript?

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.


1 Answers

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;
like image 97
Voonic Avatar answered Oct 10 '22 10:10

Voonic