Using the addEventListener()
method to attach the "resize" event on the window object.
window.addEventListener("resize", myFunction);
<!DOCTYPE html> <html> <body> <p>Try to resize the browser window.</p> <p>Window resized <span id="demo">0</span> times.</p> <script> window.addEventListener("resize", myFunction); var x = 0;/*from w w w. j a v a 2 s .c o m*/ function myFunction() { var txt = x += 1; document.getElementById("demo").innerHTML = txt; } </script> </body> </html>