Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function when window is resized

How can I call for this(or any) JS function to be run again whenever the Browser window is resized?

<script type="text/javascript">  function setEqualHeight(e) {      var t = 0;      e.each(function () {          currentHeight = $(this).height();          if (currentHeight > t) {              t = currentHeight          }      });      e.height(t)  }  $(document).ready(function () {      setEqualHeight($(".border"))  }) </script> 
like image 381
Rich Avatar asked Mar 04 '13 15:03

Rich


People also ask

How do you call a function in Windows 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.

What happens 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.

What is resize method in JavaScript?

Definition and UsageThe onresize event occurs when the browser window has been resized. Tip: To get the size of an element, use the clientWidth, clientHeight, innerWidth, innerHeight, outerWidth, outerHeight, offsetWidth and/or offsetHeight properties.


2 Answers

You can use the window onresize event:

window.onresize = setEqualHeight; 
like image 58
udidu Avatar answered Oct 12 '22 11:10

udidu


You can subscribe to the window.onresize event (See here)

window.onresize = setEqualHeight; 

or

window.addEventListener('resize', setEqualHeight); 
like image 41
dougajmcdonald Avatar answered Oct 12 '22 09:10

dougajmcdonald