Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate event resize

Tags:

javascript

dom

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?

like image 832
Simcha Avatar asked Jan 29 '26 03:01

Simcha


1 Answers

The following should work:

var resizeEvent = new Event('resize');

window.dispatchEvent(resizeEvent);

Simple (ridiculously so) demo.

References:

  • Creating and triggering (JavaScript) events.
  • Event() constructor.
like image 68
David Thomas Avatar answered Jan 30 '26 15:01

David Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!