Visible window height instead of $(window).height();

JavascriptJquery

Javascript Problem Overview


Is there any way to get the visible height of the whole page from inside an iframe, $(window).height() gives me the iframes height?

Javascript Solutions


Solution 1 - Javascript

If you are using frames, you can get the height of the outermost window by using window.top in the jQuery constructor. The height of [window.top][1] will get the height of the browser window.

$(window.top).height();

Edit: Updated window.top reference as Mozilla moved their documentation around. [1]: https://developer.mozilla.org/en/DOM/window.top

Solution 2 - Javascript

I have always used this implementation

window.innerHeight or document.body.clientHeight or document.documentElement.­clientHeight depending on the browser.

But i don't see why jquery's $(window).height() wont work for your visible height ?

Solution 3 - Javascript

I had cause to address a similiar issue today, because in FF screen.height and window.innerHeight return the same value, and of course my first response was to check for solutions on SO. In the end this is how I addressed the matter, and I'm posting the longwinded version here for posterity only...

function visibleWindowHeight( callback ) {
    /* create temporary element 'tmp' */
    var vpHeight,
        tmp = document.createElement('div');

    tmp.id = "vp_height_px";

    /* tmp height = viewport height (100vh) */
    tmp.setAttribute("style", "position:absolute;" +
        "top:0;" +
        "left:-1px;" +
        "width:1px;" +
        "height:100vh;");

    /* add tmp to page */
    document.body.appendChild(tmp);
    
    /* get tmp height in px */
    vpHeight = document.getElementById("vp_height_px").clientHeight;
    
    /* clean up */
    document.body.removeChild(tmp);
    
    /* pass value to callback and continue */
    callback( vpHeight );
}

document.addEventListener('DOMContentLoaded', function() {

    visibleWindowHeight( function( visibleHeight ) {
    
        console.log("visibleHeight:", visibleHeight);
        /*
            ... continue etc etc  ...
        */
    });

}, !1);

It might help someone, sometime. 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncurly_bracketsView Question on Stackoverflow
Solution 1 - JavascriptlonesomedayView Answer on Stackoverflow
Solution 2 - JavascriptMarutiBView Answer on Stackoverflow
Solution 3 - JavascriptBrian PeacockView Answer on Stackoverflow