Подтвердить что ты не робот

Live Detect Browser Size - jQuery/JavaScript

Есть ли плагин jQuery или способ использования прямого JavaScript для определения размера браузера.

Я бы предпочел, чтобы результаты были "живыми", поэтому, если изменяется ширина или высота, то и результаты.

4b9b3361

Ответ 1

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

Изменить: Обновлен код JavaScript для поддержки IE8 и более ранних версий.

Ответ 2

вы можете использовать

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>

Ответ 3

Это должно вернуть видимую область:

document.body.offsetWidth
document.body.offsetHeight

Я думаю, что это всегда равно размеру браузера?

Ответ 4

используйте ширину и высоту в любом месте, где угодно... при изменении размера браузера он также изменит значение переменной.

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

Ответ 5

Вы имеете в виду что-то вроде этого window.innerHeight; window.innerWidth $(window).height(); $(window).width()

Ответ 6

Вы можете попробовать добавить даже прослушиватель при повторном размере, например

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