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?
In your modern browsers, you can trigger the event using: window. dispatchEvent(new Event('resize'));
Answer: Use the addEventListener() Method You can simply use the addEventListener() method to register an event handler to listen for the browser window resize event, such as window. addEventListener('resize', ...) . The following example will display the current width and height of the browser window on resize.
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'));
}
Try this
var resizeEvent = window.document.createEvent('UIEvents');
resizeEvent .initUIEvent('resize', true, false, window, 0);
window.dispatchEvent(resizeEvent);
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With