I'm trying to inject a getStatus function into window
. Specifically I want to stick it on windows returned by window.open
:
window.open = function (open) {
return function (url, name, opts) {
var windowObj = open.call(window, url, name, opts);
windowObj.getStatus = function () {
return 1;
};
return windowObj;
};
}(window.open)
Somewhere else in my code, I do:
var myWindow = window.open("...", "...", "...");
var a = myWindow.getStatus();
Sometimes when I debug from Visual Studio 2015, I can successfully call the function, and sometimes I can't and get a crash:
0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'getStatus'
Has anyone experienced issues like this? Feels like a timing issue. Possibly an issue with the debugger.
Has anyone experienced issues like this?
No. I tried it multiple times with debugging in Visual Studio 2013 Express edition and it did not crash. I was able to debug it without any issue.
Note: with the simplified version of the question, following response may sound out of context.
Frankly speaking I had never tried overriding window's default behavior. But looking at your attempt it was very tempting to give it a try. :)
So I tried it here: https://jsfiddle.net/vnathalye/4hhrb255/
And atleast the alert(1)
is getting executed consistently. I've also modified it to call native addEventListener
and it also seems to work with following conditions:
click
event instead of close
. beforeunload
also works.AFAIK, if you are opening a different page in your child window then you can't handle it's JS events in your parent window. You will need to handle appropriate JS events in the page that is opened in child window and from those handlers you will need to pass on the information to window.opener
.
Regarding close
event, I've never used it (or even heard about it :| ). Instead unload
/ beforeunload
are the two events that are usually used.
User contributions licensed under CC BY-SA 3.0