javascript - window.onchange - What triggers it, and is it safe to remove? - Stack Overflow

admin2025-04-22  0

I've inherited the following code:

// if parent window is closed or changed, close popups
window.onbeforeunload = function () { /* some cleanup stuff */ };
window.onchange = function () { /* same exact cleanup stuff */ };

The window.onchange line is causing problems;
On Chrome, it fires for no apparent reasons (with undesirable cleanup results). It doesn't fire in IE11.

Can I safely get rid of this line?

The Mozilla guys don't give too much information:

An event handler for change events sent to the window.

...and The Internet in general doesn't seem to have much to say about it either.

So what exactly triggers windows.onchange? Does it work on all major browsers? (If not, then that's a good enough assurance for me that I can delete it.)

I've inherited the following code:

// if parent window is closed or changed, close popups
window.onbeforeunload = function () { /* some cleanup stuff */ };
window.onchange = function () { /* same exact cleanup stuff */ };

The window.onchange line is causing problems;
On Chrome, it fires for no apparent reasons (with undesirable cleanup results). It doesn't fire in IE11.

Can I safely get rid of this line?

The Mozilla guys don't give too much information:

An event handler for change events sent to the window.

...and The Internet in general doesn't seem to have much to say about it either.

So what exactly triggers windows.onchange? Does it work on all major browsers? (If not, then that's a good enough assurance for me that I can delete it.)

Share Improve this question asked Oct 25, 2016 at 15:59 Yehuda ShapiraYehuda Shapira 8,6306 gold badges46 silver badges66 bronze badges 1
  • 1 My understanding is that window does not have an onchange event. I'm not sure what is triggering it. I would try removing it and test functionality. Assuming everything is okay, leave it out. – user1289451 Commented Oct 25, 2016 at 16:04
Add a ment  | 

1 Answer 1

Reset to default 2

Change events fire when a form control is changed. In the demo below, type something, then hit Tab to blur the control and trigger the change event.

onchange = function (e) { console.log(e.target); }
<input>

Events bubble, so the listener does not need to be directly attached to the input.


It's impossible to say if it is "safe to remove" in your example, because there is no context provided about what is being cleaned up so we can't tell if it is necessary / useful / harmful to perform the clean up after every change event.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745268955a293456.html

最新回复(0)