Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire window.onresize event?

Tags:

javascript

dom

I am trying to fire the resize event on window programmatically:

var evt = document.createEvent('HTMLEvents');
evt.initEvent('resize', true, false);
window.dispatchEvent(evt);

That unfortunately does not work. I am trying to make ExtJS library to do recalculations, and it uses DOM Level 3 events so I cannot just call window.onresize();.

like image 290
Tower Avatar asked Oct 01 '11 17:10

Tower


2 Answers

You are creating the wrong event.

Resize is a UIEvent, read the specs here

You need to do this:

var evt = document.createEvent('UIEvents');
evt.initUIEvent('resize', true, false,window,0);
window.dispatchEvent(evt);

See it in action here: http://jsfiddle.net/9hsBA/

like image 161
Martin Jespersen Avatar answered Oct 12 '22 22:10

Martin Jespersen


In case createEvent might get deprecated in favor of CustomEvent, here's a similar snippet using the CustomEvent API:

var ev = new CustomEvent('resize'); //instantiate the resize event
ev.initEvent('resize');

window.dispatchEvent(ev); // fire 'resize' event!
like image 22
yuvilio Avatar answered Oct 12 '22 23:10

yuvilio