Although it's 2023, many developers still rely on the jQuery library for its ease of use in JavaScript. If you need to obtain the screen width and height of your browser window using jQuery, look no further than the following code. As you resize your browser window, the width and height values of the frame are automatically displayed.
There could be various reasons one may want to get the width and height of a web browser,
- In order to create a responsive website that adapts to the different screen sizes (mobile/laptops), you need to know the screen width and height so that you can adjust the layout accordingly.
- Knowing the screen size of the user could help you can customize the user experience for different devices. For example, you might want to display a mobile-friendly version of your site on small screens or show a larger version of images on larger screens.
- This may also help to track the screen size of your visitors to help you analyze their behavior and preferences. For example, you might find that visitors using smaller screens tend to click on certain areas of your site more frequently than those using larger screens.
- If you display ads on your website, it could be so that you need to know specific screen sizes for displaying ads, so by knowing the screen size, you can ensure that the appropriate ad is displayed to the user.
This code can be quite handy when testing the responsive design of your website. You can view this page on various devices to get the height and width.
Code Example:<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.10.2.min.js" >
</script>
<style>
#display {
width: auto;
height:22px;
background-color: #ccc;
color: #222;
margin-left: 5px;
font-size:20px;
text-align: center;
}
</style>
</head>
<body>
<script>
$("#display").html("<b>Width</b> : " +
$(window).width() + ",<b>Height : </b>" +
$(window).height());
$(window).resize(function() {
$("#display\").html("<b>Width</b> : " +
$(window).width() + ",<b>Height : </b>" +
$(window).height());
});
</script>
<div id="display"></div>
</body>
</html>
You can see the below gif file - you can see that as we resize the browser window, we get the correct width and height size in pixels.

⛏️ Note: There is an easier way to do this using the Chrome browser Inspect option, Right Click on any web page and click on Inspect, click on "Toggle Device Toolbar" and select the responsive option, drag the width and height to know how your page renders and what the page dimensions are.
Facing issues? Have Questions? Post them here! I am happy to answer!
- How to check if an element is hidden using jQuery code?
- Disable jQuery button after being click
- Make div element draggable using jQuery
- Get Browser Screen Width and Height dynamically using jQuery
- jQuery : Move to top of the page
- [jQuery] Uncaught ReferenceError: $ is not defined at index.html:5
- Redirect page using jQuery
- What does chmod +x filename command do? - Linux
- Minecraft Java Edition - Java
- All directional arrows codes for HTML - Html
- How to migrate Kaizala to Microsoft Teams - Teams
- Display Output in Java Console as a Table - Java
- How to check if an element is hidden using jQuery code? - jQuery
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete - Android
- Add imports in eclipse shortcut key combinations - Eclipse