I'm trying to trigger resize event using plain js for testing purpose, but window.resizeTo()
and window.resizeBy()
don't trigger the event according to the modern browsers prevent these actions. I tried jquery $(window).trigger('resize');
but it works only for events that attached via jquery like $(window).on('resize', handler);
. however in my case the project I am working on uses plain javascript.
example
window.addEventListener('resize', function(){
console.log('window has been resized !');
});
// or even using global onresize
window.onresize = function() {
console.log('window has been resized!');
};
Press Alt + Spacebar again to open the window menu, arrow down to Size, and press Enter . Press the up or down arrow key if you want to resize the window vertically or the left or right arrow key if you want to resize horizontally.
The resize event fires when the document view (window) has been resized. This event is not cancelable and does not bubble. In some earlier browsers it was possible to register resize event handlers on any HTML element.
The resizeTo() method is used to resizes a window to the specified width and height. Parameter Values: width: Sets the width of the window, in pixels. height: Sets the height of the window, in pixels.
In pure JavaScript, you can trigger the resize event using window.dispatchEvent () method: alert('Handler for .resize () called!'); Alternatively, you can use the window.resizeTo () to trigger the resize event. alert('Handler for .resize () called!'); That’s all about triggering the window’s resize event in JavaScript and jQuery.
You should avoid using the solution like window.onresize = function (event) { ... };, since it overrides the window.onresize event handler function. You should better assign a new event handler to the resize event using event listener, as shown in the example above. Please check out the tutorial on JavaScript event listeners to learn more about it.
If the resize event is triggered too many times for your application, see Optimizing window.onresize to control the time after which the event fires. The following example reports the window size each time it is resized.
jQuery's trigger does not actually trigger the native "resize" event. It only triggers event listeners that have been added using jQuery. In my case, a 3rd party library was listening directly to the native "resize" event and this is the solution that worked for me. Great and simple solution.
You are most likely getting downvoted as people will see it as a duplicate, however from the you might not need jquery website:
IE9+
Trigger native
// For a full list of event types: https://developer.mozilla.org/en-US/docs/Web/API/document.createEvent
var el = document; // This can be your element on which to trigger the event
var event = document.createEvent('HTMLEvents');
event.initEvent('resize', true, false);
el.dispatchEvent(event);
Trigger custom
var el = document; // This can be your element on which to trigger the event
if (window.CustomEvent) {
var event = new CustomEvent('my-event', {detail: {some: 'data'}});
} else {
var event = document.createEvent('CustomEvent');
event.initCustomEvent('my-event', true, true, {some: 'data'});
}
el.dispatchEvent(event);
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