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?
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.