Cross-browser window resize event using JavaScript/jQuery Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The resize() method is an inbuilt method in jQuery that is used when the browser window changes its size. The resize() method triggers the resize event or attaches a function to run when a resize event occurs. jQuery has a built-in method for window resizing events. Syntax: $(selector).resize(function) This syntax is used for cross-browser resize events. Example 1: This example uses resize() method to count how many times browsers window resize. html <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <script> var x = 1; $(document).ready(function() { $(window).resize(function() { $("span").text(x += 1); }); }); </script> <p>Window resized <span>0</span> times.</p> <p>Try resizing your browser window.</p> Output: You can use the addEventListener() method to register an event handler to listen for the browser window resize event, such as window.addEventListener('resize', ...). Syntax: object.addEventListner("resize", myscript); Example 2: This example display the size of resize windows. html <div id="result"></div> <script> // Defining event listener function function displayWindowSize() { // Get width and height of the window // excluding scrollbars var w = document.documentElement.clientWidth; var h = document.documentElement.clientHeight; // Display result inside a div element document.getElementById("result").innerHTML = "Width: " + w + ", " + "Height: " + h; } // Attaching the event listener function // to window's resize event window.addEventListener("resize", displayWindowSize); // Calling the function for the first time displayWindowSize(); </script> <p> <strong>Note:</strong> Please resize the browser window to see how it works. </p> Output: Comment More info D divyanksingh200 Follow Improve Article Tags : JavaScript Technical Scripter 2019 JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like