『壹』 js里面获取屏幕宽高的screen对象,为什么获取不了屏幕的宽高值
<!DOCTYPEhtml>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>
RunJS演示代码
</title>
<styletype="text/css">
body,div{
margin:0px;
padding:0px;
}
#show{
width:400px;
height:400px;
margin:100pxauto;
background:#969696;
text-align:center;
}
</style>
<scripttype="text/javascript">
functionmess(){
varstr="屏幕尺寸基本信息:<br/>";//这里你是中文的分号,故而报错
str+="屏幕宽度:"+screen.width+"像素<br/>";
str+="屏幕高度:"+screen.height+"像素<br/>";
str+="屏幕可用宽度"+screen.availWidth+"像素<br/>";
str+="屏幕可用高度"+screen.availHeight+"像素<br/>";
document.getElementById("show").innerHTML=str;
}
</script>
</head>
<bodyonload="mess()">
<divid="show">
</div>
</body>
</html>
『贰』 JavaScript中如何获取手机浏览器的大小
JavaScript不管是判断PC浏览器还是手机浏览器,都是通过User Agent 来判断
var ua = navigator.userAgent;
取得大小可内以试下
var screenAW = window.screen.availWidth;
var screenAH = window.screen.availHeight;
或者容
var screenW = window.screen.width;
var screenH = window.screen.height;