

For example, let’s add an event to the object window to get its width and height and show it on the web page. The onresize event occurs when the browser window has been resized. This function is used to specify what will happen if the size of a window is being changed. To add a resize event to the window, we can use the onresize() function in JavaScript. Add a Resize Event Using the onresize() Function in JavaScript You can also use the addEventListener() function to add an event to any object, like a checkbox. In some earlier browsers it was possible to register resize event handlers on any HTML element. This event is not cancelable and does not bubble. You can save the above code into an HTML file and open it with any browser and change its size to see if the code works or not. The resize event fires when the document view (window) has been resized. The width and height of the window will be shown on the page, and it will change as the size of the window is changed.

After the start() function, we added the resize event, which will call the start() function when a user resizes the window. In the script tag, we have a function start(), which is used to change the text of the two spans with the width and height of the window. low battery or media events from the operating system), and other causes.

The following example will display the current width and height of the browser window on resize. These can arise from user interactions such as using a mouse or resizing a window, changes in the state of the underlying environment (e.g. 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'. Tip: To get the size of an element, use the clientWidth, clientHeight, innerWidth, innerHeight, outerWidth, outerHeight, offsetWidth and/or offsetHeight properties. Event reference Events are fired to notify code of 'interesting changes' that may affect code execution. The id of the span will be used to get the element in JavaScript. The onresize event occurs when the browser window has been resized. We added a br tag to move the cursor to a new line, and on the new line, we added another span with text Height=, and after that, we added another empty span with an id SpanID2. In the above code, we added a span with text Width=, and after that, we added an empty span with an id SpanID1 in the body section. querySelector ( 'input' ) const checkbox = document. querySelector ( "body > div" ) const slider = document.

querySelector ( "p" ) const divElem = document. querySelector ( "h1" ) const pElem = document.
