jquery - Overriding window.close in Javascript - Stack Overflow

admin2025-03-31  6

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?

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?

Share Improve this question edited Sep 27, 2022 at 21:47 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 22, 2013 at 12:52 Rajesh DhimanRajesh Dhiman 1,8961 gold badge18 silver badges30 bronze badges 9
  • 6 Have you tried it to see what happens? – Lloyd Commented Aug 22, 2013 at 12:53
  • yes, nothing happens when I close the new window. – Rajesh Dhiman Commented Aug 22, 2013 at 12:55
  • did you look at window.unload? – fred02138 Commented Aug 22, 2013 at 12:56
  • 6 If you could do this, it would be possible to create windows that could never be closed. Malware sites did this, so browsers don't allow it. – Barmar Commented Aug 22, 2013 at 12:57
  • 4 Oh yes, please give me more popups when I try to close your advertising page that I didn't ask for in the first place. – crush Commented Aug 22, 2013 at 12:57
 |  Show 4 more ments

1 Answer 1

Reset to default 6

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;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743410796a213011.html

最新回复(0)