Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger window resize event using vanilla javascript?

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!');
};
like image 688
Mohamed Hassan Avatar asked Aug 30 '16 21:08

Mohamed Hassan


People also ask

How do I force a window to resize?

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.

What happen when window is resized?

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.

How do I resize a browser window in HTML?

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.

How to trigger the window resize event in JavaScript and jQuery?

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.

How to avoid using window OnResize () function in JavaScript?

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.

What happens if the resize event is triggered too many times?

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.

How to trigger the native resize event with jQuery trigger?

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.


1 Answers

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);
like image 66
bitten Avatar answered Nov 15 '22 14:11

bitten