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

I've inherited the following code: if parent window is closed or changed, close popupswindow.on

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745268953a4619627.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信