Live detect and print browser/iframe resolution with jQuery and JavaScript
by katalin_2003
HTML
<!-- JavaScript -->
<p>
<strong>JavaScript resize:</strong>
<span id="jsWidth">0</span> x
<span id="jsHeight">0</span>
</p>
<br/>
<!-- jQuery -->
<p>
<strong>jQuery resize:</strong><br />
Width: <span id="jqWidth">0</span>
<br />Height: <span id="jqHeight">0</span>
</p>
JavaScript
// 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;
document.getElementById('jsHeight').innerHTML = 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);
$('#jqHeight').html(height);
};
$(document).ready(jqUpdateSize); // When the page first loads
$(window).resize(jqUpdateSize); // When the browser changes size