• 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 Add Items to Set
  • 2.  Using JavaScript to Get the Page Title
  • 3.  Using JavaScript to Add to Array
  • 4.  Remove Undefined Values From an Array in JavaScript
  • 5.  JavaScript onkeydown – How to Use onkeydown Event with JavaScript
  • 6.  Math floor JavaScript – Using Math.floor() to Round Down to Floor
  • 7.  Using JavaScript to Remove the First Character From a String
  • 8.  Get a List of Prime Numbers Using JavaScript
  • 9.  Get the First Child Element and Change it with JavaScript
  • 10.  Swapping Images in JavaScript

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

x