How to trigger the window resize event in JavaScript?

zhongshu picture zhongshu · Nov 30, 2009 · Viewed 438.7k times · Source

I have registered a trigger on window resize. I want to know how I can trigger the event to be called. For example, when hide a div, I want my trigger function to be called.

I found window.resizeTo() can trigger the function, but is there any other solution?

Answer

avetisk picture avetisk · Sep 9, 2013
window.dispatchEvent(new Event('resize'));