1<!DOCTYPE html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<title>JavaScript Window Resize Event</title>
6</head>
7<body>
8<div id="result"></div>
9
10<script>
11// Defining event listener function
12function displayWindowSize(){
13 // Get width and height of the window excluding scrollbars
14 var w = document.documentElement.clientWidth;
15 var h = document.documentElement.clientHeight;
16
17 // Display result inside a div element
18 document.getElementById("result").innerHTML = "Width: " + w + ", " + "Height: " + h;
19}
20
21// Attaching the event listener function to window's resize event
22window.addEventListener("resize", displayWindowSize);
23
24// Calling the function for the first time
25displayWindowSize();
26</script>
27<p><strong>Note:</strong> Please resize the browser window to see how it works.</p>
28</body>
29</html>