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?

9
  • 6
    Have you tried it to see what happens? Commented Aug 22, 2013 at 12:53
  • yes, nothing happens when I close the new window. Commented Aug 22, 2013 at 12:55
  • did you look at window.unload? 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. 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. Commented Aug 22, 2013 at 12:57

1 Answer 1

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; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.