Javascript 能獲得解析度以及各種高度寬度,在Firefox、IE、Chrome瀏覽器下結果不相同。
總結:螢幕解析度是可靠的,用window.screen.height + ‘x’ + window.screen.width。
捲軸滾了多少,只有chrome知道。
網頁高度是可靠的,用document.body.scrollHeight。
網頁寬度不能用document.body.scrollWidth。
瀏覽器有多寬,IE6不知道,其他瀏覽器知道。
瀏覽器有多高,都不知道。
還有一點,IE、Firefox整體放大是類比小解析度,1600x900放大125%,js檢測到為1280x720。也就是說流量統計中的IE、Firefox解析度不再可靠。Chrome整體放大後解析度依然可靠。
線上效果:http://web-developer-tips.appspot.com/javascript_screen_width/
代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>javascript 顯示器解析度</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body
{
margin:0;
padding:0;
}
</style>
</head>
<body>
<div style="width:3000px;height:2000px;">
<h1>javascript 顯示器解析度</h1>
<div style="position:fixed;left:20px;top:50px;background:#FFCC00;" id="content">
</div>
</div>
<script type="text/javascript">
//<![CDATA[
//var font_size = screen.width / 64 + 'px';
//document.body.style.fontSize = font_size;
//var p = document.createElement("p");
//p.innerHTML = "解析度" + screen.width + 'x' + screen.height;
//alert("解析度" + screen.width + 'x' + screen.height);
function showWidth()
{
var s = " ";
s += "<p>網頁可見地區寬: "+ document.documentElement.clientWidth;
s += "</p><p>網頁可見地區高: "+ document.documentElement.clientHeight;
s += "</p><p>網頁可見地區寬: "+ document.body.offsetWidth + " (包括邊線和捲軸的寬) ";
s += "</p><p>網頁可見地區高: "+ document.body.offsetHeight + " (包括邊線的寬) ";
s += "</p><p>網頁本文全文寬: "+ document.body.scrollWidth;
s += "</p><p>網頁本文全文高: "+ document.body.scrollHeight;
s += "</p><p>網頁被捲去的高: "+ document.body.scrollTop;
s += "</p><p>網頁被捲去的左: "+ document.body.scrollLeft;
s += "</p><p>網頁本文部分上: "+ window.screenTop;
s += "</p><p>網頁本文部分左: "+ window.screenLeft;
s += "</p><p>螢幕解析度的高: "+ window.screen.height;
s += "</p><p>螢幕解析度的寬: "+ window.screen.width;
s += "</p><p>螢幕可用工作區高度: "+ window.screen.availHeight;
s += "</p><p>螢幕可用工作區寬度: "+ window.screen.availWidth;
s += "</p><p>你的螢幕設定是 "+ window.screen.colorDepth + " 位彩色 ";
s += "</p><p>你的螢幕設定 "+ window.screen.deviceXDPI + " 像素/英寸 </p>";
document.getElementById('content').innerHTML = s;
}
window.onload = function()
{
showWidth();
}
window.onresize = function()
{
showWidth();
}
window.onscroll = function()
{
showWidth();
}
//]]>
</script>
</body>
</html>
:
asdf