<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script type="text/javascript">
document.write("当前窗口宽:"+document.body.clientWidth+"<br />"+"当前窗口高:"+document.body.clientHeight);
</script>
</body>
</html>
因为你body没有内容,body没有被撑开高度就是0;
注意:document.write()添加的内容不会实际写入body中
你在body中随便打些数字就有高度了
不兼容;
兼容写法:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script type="text/javascript">
var w=document.body.clientWidth||document.documentElement.clientWidth;
var h=document.body.clientHeight||document.documentElement.clientHeight;
document.write("当前窗口宽:"+w+"<br>"+"当前窗口高:"+h);
</script>
</body>
</html>