10

I want to call the resize event using code.

I am using following code .It is working fine in otherbrowsers but not in the IE11.

if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
            $(window).trigger('resize');

        } else {
            window.dispatchEvent(new Event('resize'));

        }

Please advice me, am I missing anything?

tanguy_k
  • 11,307
  • 6
  • 54
  • 58
Naju
  • 1,541
  • 7
  • 27
  • 59
  • Just use `$(window).trigger('resize')` it will work in IE 11 also – Satpal Jun 01 '15 at 10:46
  • @Satpal nope,It is not working :( – Naju Jun 01 '15 at 10:48
  • 1
    @rightPath Provide minimalistic sample to replicate your issue in question itself. And so post code binding event you are trying to trigger or better explain what you are looking for, don't just say `it is not working`... – A. Wolff Jun 01 '15 at 10:50

3 Answers3

31

Even Internet explorer 11 does not support resize event. Therefore, I have resolved this by using following solution.

if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
     var evt = document.createEvent('UIEvents');
     evt.initUIEvent('resize', true, false, window, 0);
     window.dispatchEvent(evt);
    } else {
       window.dispatchEvent(new Event('resize'));

    }
Shivek Parmar
  • 2,865
  • 2
  • 33
  • 42
1

Try this

var resizeEvent = window.document.createEvent('UIEvents'); 
resizeEvent .initUIEvent('resize', true, false, window, 0); 
window.dispatchEvent(resizeEvent);
teacoffee
  • 11
  • 2
1

The solution from the other post How to trigger the window resize event in JavaScript? that I came across works well.

Snippet of the same:

if (typeof(Event) === 'function') {
    // modern browsers
     window.dispatchEvent(new Event('resize'));
} else {
    // for IE and other old browsers
    // causes deprecation warning on modern browsers
    var evt = window.document.createEvent('UIEvents'); 
    evt.initUIEvent('resize', true, false, window, 0); 
    window.dispatchEvent(evt);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
saddy
  • 11
  • 3