To return the live width of the current window in Javascript, use a property of the window that is innerWidth.
Step: 1 Write HTML documentation and use a script tag to code in Javascript.
Step: 2 Pass onresize event on the window. Now, the code in the onresize event function will execute as many times as the window resizes.
Step: 3 You can use any Javascript selector to print the result in. Here I am directly printing the result in the body. So, I have selected the body like this document.body
Step: 4 Use innerHTML output property to print the result in the selected DOM (Document Object Modal).
Step: 5 For window width, use the innerWidth property to return the current width of the window.
<!DOCTYPE html>
<html>
<head>
<title>How to get width of screen when window is resizing?</title>
</head>
<body>
<script>
window.onresize=function()
{
document.body.innerHTML=window.innerWidth;
}
</script>
</body>
</html>
If you want to know the window width when the browser window is loaded then simply use the window.onload event.
<!DOCTYPE html>
<html>
<head>
<title>How to get width of screen when window is resizing?</title>
</head>
<body>
<script>
window.onload=function()
{
document.body.innerHTML=window.innerWidth;
}
</script>
</body>
</html>
That's it. Hope, you understood. write a comment if you have any doubt.
Publish A Comment