All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Window Height In jQuery

Last Updated : Mar 11, 2024

How To Get Window Height In jQuery

In this article we will show you the solution of how to get window height in jquery, here we are going to show you example to achieving result with the help of window object and height() method. The window object allow you to calculate window width or height as per your need.

The height() method used for used to retrieve current height of particular object. Here we need to join these two to get current window size even if you resize it refresh the result then you can see correct result.

Step By Step Guide On How To Get Window Height In jQuery :-

Here we defined html block with heading h2 tag, p tag with line of text for user verification, h4 for append result and button ‘Get Height’ for proceed process.

In script block we defined ready() method which is automatically loads program when open browser within that click method defined with button by specified class name.

There we defined result text with current window height then appends with h4 tag for display on webpage.

<!DOCTYPE html>
<html>
    <head>
        <title>WINDOW HEIGHT</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $(".get_window_height").click(function(){
                    $("h4").text("Window Height Is : "+$(window).height());
                })
            })
        </script>
    </head>
    <body>
        <div>
            <h2>Welcome All</h2>
            <p>Click Button <b><i>"Get Height"</i></b> Then Will Display Window Size</p>
            <h4></h4>
        </div>
        <button class="get_window_height">Get Height</button>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() function, within that we appending click() with button ‘Get Height’ by specifying class name ‘get_window_height’.
  8. There we specified result text ‘Window Height Is’ with ‘$(window).height()’ line, which help you to retrieve current window height then we appends with it. Final result bind with h4 tag for display on body of webpage.
  9. In body block we defined heading ‘Welcome All’, p tag with text ‘Click Button <b><i>"Get Height"</i></b> Then Will Display Window Size’ which refers instruction for users.
  10. Empty h4 tag for display dynamic result and button ‘Get Height’ defined with class ‘get_window_height’.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get window height in jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes in document without any error.

When we executes this program on browser we can see heading , instruction, button, now user needs to clicks on it when to see retrieved recent height of window on webpage.

I hope this article on how to get window height in jquery helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪