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?
- 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
1 Answer
Reset to default 6Try 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;