Diagonal Screensize Calculator
This script is handy for determining screen size on the diagonal. Conventional models do not account for mobile and tablet tilting.
Another note, most screen sizes are calculated on the diagonal. This script returns that value for what ever shape the screen might take and is a more reactive approach to calculating screen dimension.
JavaScript
// Author Name: Pete Fecteau
// Author URL: http://buttonpresser.com
var winW = screen.innerWidth;
var winH = window.innerHeight;
var pyth = (winW*winW)+(winH*winH); // Plugs visible window width (winW) and height (winH) into pythagorian theorym (A*A + B*B = C*C)
var diag = Math.round(Math.sqrt(pyth)/72*10)/10; // Find square root of pyth, divide by 72 (resolution), round it off. Final value is in inches.
/*
// Cross browser visible window detection
// Not necessary if not supporting < IE9
if (document.body && document.body.offsetWidth) {
winW = document.body.offsetWidth;
winH = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
document.documentElement &&
document.documentElement.offsetWidth ) {
winW = document.documentElement.offsetWidth;
winH = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
winW = window.innerWidth;
winH = window.innerHeight;
}
*/
document.write(Math.round(winW/72*10)/10+'" Width<br>');
document.write(Math.round(winH/72*10)/10+'" Height<br>');
document.write(diag+'" Diagonal<br>');
if (winW > winH){
document.write("Landscape<br>");
}
else if (winH > winW)
document.write("Portrait<br>");
else {
document.write("Square<br>");
};
if (diag <= 4.5){ // Mobile devices range from ~2.2" to ~4.5"
document.write("Mobile");
}
else if (diag > 4.5 && diag <= 10){ // Tablets are from ~4.5" to ~10"
document.write("Tablet");
}
else if (diag > 20){ // Anything larger than 20" and you may want to rethink your UI
document.write("Oversized");
}
else { // Typical laptop/desktop displays are between ~10" to ~20"
document.write("Laptop/Desktop");
};