-->

Simulate event resize

2020-07-25 09:22发布

问题:

Hi I'm using DOM2 and added a few event listeners "resize" for window object, using addEventListener. And now I need to simulate this action (window.resize) to call event listeners that are attached to the window object. How can I do it?

回答1:

The following should work:

var resizeEvent = new Event('resize');

window.dispatchEvent(resizeEvent);

Simple (ridiculously so) demo.

References:

  • Creating and triggering (JavaScript) events.
  • Event() constructor.