1. 程式人生 > >html5 獲取頁面的寬和高

html5 獲取頁面的寬和高

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
<script type="text/javascript">
<!--

function findDimensions() //函式:獲取尺寸
		{
			var winWidth = 0;
			var winHeight = 0;
			//獲取視窗寬度
			if (window.innerWidth){
				// console.log(1);
				 winWidth = window.innerWidth;}
			else if ((document.body) && (document.body.clientWidth))
				winWidth = document.body.clientWidth;
			//獲取視窗高度
			if (window.innerHeight)
				winHeight = window.innerHeight;
			else if ((document.body) && (document.body.clientHeight))
				winHeight = document.body.clientHeight;
				//通過深入Document內部對body進行檢測,獲取視窗大小
			if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)
			{
				winHeight = document.documentElement.clientHeight;
				winWidth = document.documentElement.clientWidth;
			}
			//結果輸出至兩個文字框
				console.log("   w "+winHeight+"   h "+winWidth);
				console.log();
				//alert(winWidth);
		}


	findDimensions();
	//呼叫函式,獲取數值
	window.onresize=findDimensions;
//-->
</script>
	
</body>
</html>
這個方法相容ie5以上版本