• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Detect Window Resize

Using JavaScript to Detect Window Resize

August 19, 2022 Leave a Comment

We can use JavaScript to detect a window resize by using the addEventListener() method on the window object.

window.addEventListener("resize", function(event){
  //Do something when the user resizes the window
});

Let’s see a simple example of this below.


Let’s say we have the following JavaScript code:

window.addEventListener("resize", function(event){
  alert("The window has been resized.");
});

No matter what our HTML code is, the alert box will show if the user ever resizes their browser window. The function will also run every time the user resizes the window.

Let’s take a look at this for ourselves in the example below.

Using JavaScript to Detect User Window Resize

In this example, we will have a div with a background color. When we resize the window, we will change the color of our box div.

Here is our HTML setup:


<style>#div1 { height: 300px; width: 300px; border: 1px solid #444; background: #7bbfa2; }</style>
<div id="div1"></div>
<p>Resize the window to change the color of the box.</p>
<p>Notice every time the box color changes when you drag the corner of the browser window to resize it.</p>

When we resize the window, we will generate a random color and then change the background color of the div.

We will detect the window resize using the addEventListener() method.

Here is our JavaScript code:


//The genRandomColor function below will generate a random color for us
function genRandomColor() {
  var letters = '0123456789ABCDEF';
  var randomColor = '#';
  for (var i = 0; i < 6; i++) {
    randomColor += letters[Math.floor(Math.random() * 16)];
  }
  return randomColor;
}

//When a resizing of the browser is detected, the function below will run
//It will run every time the browser is resized.
window.addEventListener("resize", function(event){
  document.getElementById("div1").style.backgroundColor = genRandomColor();
});

The final code and output for this example of how to detect and change some HTML on window resize is below:

Code Output:

Resize the window to change the color of the box.

Notice every time the box color changes when you drag the corner of the browser window to resize it.

Full Code:


<style>#div1 { height: 300px; width: 300px; border: 1px solid #444; background: #7bbfa2; }</style>
<div id="div1"></div>
<p>Resize the window to change the color of the box.</p>
<p>Notice every time the box color changes when you drag the corner of the browser window to resize it.</p>

<script>

function genRandomColor() {
  var letters = '0123456789ABCDEF';
  var randomColor = '#';
  for (var i = 0; i < 6; i++) {
    randomColor += letters[Math.floor(Math.random() * 16)];
  }
  return randomColor;
};

window.addEventListener("resize", function(event){
  document.getElementById("div1").style.backgroundColor = genRandomColor();
});

</script>

Hopefully this article has helped you to understand about JavaScript window resize.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Redirect After 5 Seconds
  • 2.  How in JavaScript to Get the Day of the Week
  • 3.  JavaScript Exponent – Exponentiate Numbers with Math.pow()
  • 4.  Using JavaScript to Change the href of a Link
  • 5.  JavaScript Print Array – Printing Elements of Array to Console
  • 6.  Using JavaScript to Change the Font Size of a Paragraph
  • 7.  Using JavaScript to Wait 5 Seconds Before Executing Code
  • 8.  Using JavaScript to Convert Month Number to Name
  • 9.  Using JavaScript to Set the Width of a Div
  • 10.  Using JavaScript to Convert a String to Uppercase

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy