Window Resize Event in JavaScript
-
Add a Resize Event Using the
addEventListener()
Function in JavaScript -
Add a Resize Event Using the
onresize()
Function in JavaScript
This tutorial will discuss adding a resize event using the addEventListener()
and onresize()
functions in JavaScript.
Add a Resize Event Using the addEventListener()
Function in JavaScript
To add a resize event to the window, we can use the addEventListener()
function in JavaScript. This function adds an event that contains a function to an object. For example, let’s add an event to the object window to get its width and height and show it on the web page. See the code below.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<span>Width = </span><span id="SpanID1"></span>
<br />
<span>Height = </span><span id="SpanID2"></span>
<script type="text/javascript">
start();
window.addEventListener('resize', start);
function start(){
document.getElementById('SpanID1').innerText = document.documentElement.clientWidth;
document.getElementById('SpanID2').innerText = document.documentElement.clientHeight;
}
</script>
</body>
</html>
Output:
Width = 798
Height = 779
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. 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
.
The id of the span will be used to get the element in JavaScript. 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. After the start()
function, we added the resize event, which will call the start()
function when a user resizes the window. 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. 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. You can also use the addEventListener()
function to add an event to any object, like a checkbox.
Add a Resize Event Using the onresize()
Function in JavaScript
To add a resize event to the window, we can use the onresize()
function in JavaScript. This function is used to specify what will happen if the size of a window is being changed. For example, let’s add an event to the object window to get its width and height and show it on the web page. See the code below.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<span>Width = </span><span id="SpanID1"></span>
<br />
<span>Height = </span><span id="SpanID2"></span>
<script type="text/javascript">
start();
window.onresize = start;
function start(){
document.getElementById('SpanID1').innerText = document.documentElement.clientWidth;
document.getElementById('SpanID2').innerText = document.documentElement.clientHeight;
}
</script>
</body>
</html>
Output:
Width = 798
Height = 779
In the above code, the start()
function will be called if the size of the window is being changed. As you can see, the output is the same as of the above method.