Live Detect Browser Size - jQuery / JavaScript

Is there a jQuery plugin or a way using straight JavaScript to detect browser size.

I'd prefer it is the results were 'live', so if the width or height changes, so would the results.

Answers:

Answer

JavaScript

function jsUpdateSize(){
    // Get the dimensions of the viewport
    var width = window.innerWidth ||
                document.documentElement.clientWidth ||
                document.body.clientWidth;
    var height = window.innerHeight ||
                 document.documentElement.clientHeight ||
                 document.body.clientHeight;

    document.getElementById('jsWidth').innerHTML = width;  // Display the width
    document.getElementById('jsHeight').innerHTML = height;// Display the height
};
window.onload = jsUpdateSize;       // When the page first loads
window.onresize = jsUpdateSize;     // When the browser changes size

jQuery

function jqUpdateSize(){
    // Get the dimensions of the viewport
    var width = $(window).width();
    var height = $(window).height();

    $('#jqWidth').html(width);      // Display the width
    $('#jqHeight').html(height);    // Display the height
};
$(document).ready(jqUpdateSize);    // When the page first loads
$(window).resize(jqUpdateSize);     // When the browser changes size

jsfiddle demo

Edit: Updated the JavaScript code to support IE8 and earlier.

Answer

you can use

function onresize (){
   var h = $(window).height(), w= $(window).width();
   $('#resultboxid').html('height= ' + h + ' width: ' w);
}
 $(window).resize(onresize ); 

 onresize ();// first time;

html:

<span id=resultboxid></span>
Answer

This should return the visible area:

document.body.offsetWidth
document.body.offsetHeight

I guess this is always equal to the browser size?

Answer

use width and height variable anywhere you want... when ever browser size change it will change variable value too..

$(window).resize(function() {
    width = $(this).width());
    height = $(this).height());
});
Answer

Do you mean something like this window.innerHeight; window.innerWidth $(window).height(); $(window).width()

Answer

You can try adding even listener on re-size like

window.addEventListener('resize',CheckBrowserSize,false);
function CheckBrowserSize() 
{
    var ResX= document.body.offsetHeight;
    var ResY= document.body.offsetWidth;
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.