1. 程式人生 > >javascript中的window物件的知識點

javascript中的window物件的知識點

javascript中的window物件的知識點

內建物件定義:可以不需要宣告和建立,可以直接使用,內建物件全部為javascript關鍵字

window是內建物件,因為window使用的太頻繁了,所以一般window可以省略不寫,我們平時用的alert()其本質就是window.alert();所以內建物件呼叫屬性和方法的時候可以不用寫物件名,而直接寫方法名和屬性名document.getElementById("xxx");等效於window.document.getElementById("xxx");

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>javascript中的window物件的知識點,雪豹軟體工作室,javascript</title>
<link rel="stylesheet" type="text/css" href="mark.css">
<link rel="stylesheet" type="text/css" href="input2.css">
<link rel="stylesheet" type="text/css" href="body.css">

<script type="text/javascript">
	/*內建物件定義:可以不需要宣告和建立,可以直接使用,內建物件全部為javascript關鍵字*/
	
	/*
	window是內建物件,因為window使用的太頻繁了,所以一般window可以省略不寫,我們平時用的alert()其本質就
	是window.alert();所以內建物件呼叫屬性和方法的時候可以不用寫物件名,而直接寫方法名和屬性名
	document.getElementById("xxx");等效於window.document.getElementById("xxx");
	*/
	function testWindow() {
		alert("test");
		window.alert("hello");
		var str = "123";
		alert(str + 12);
		alert(parseInt(str)+ 12);
		/*
		這裡有個知識點(瞭解Javascript函式:parseInt(),參考https://www.2cto.com/kf/201304/203710.html或
		者參考https://yq.aliyun.com/ziliao/72451 ),暫時沒時間研究,以後再深究吧
		*/
		//parseInt()本質上是window.parseInt(),即parseInt()等效於window.parseInt()
		alert(parseInt(str)+ 12 + window.parseInt("1"));
		//alert("href = " + window.location.href);
		//alert("location = " + window.location);
		//alert("href = " + location.href);
	}
	
	function testAlert(){
		window.alert("測試window.alert方法");
	}
	
	function testConfirm(){
		if (window.confirm("您確定嗎?")) {
			alert("您選擇了確定!");
		}else {
			alert("您選擇了取消!");
		}
	}
	
	function testOpen(){
		//新視窗開啟
		//window.open("http://www.sogou.com");
		//window.open("http://www.sogou.com", "_self");
		//window.open("calculator.html");
		//window.status = "測試哈哈"; //狀態列顯示文字
		/*
		for (var count = 1; count <= 5; count++) {
			window.open("calculator.html");
		}
		*/
		window.open("calculator.html", "test", "toolbars=1, location=0, statusbars=0, menubars=0,width=500,height=500,scrollbars=1")
	}
	
	function testStatus(){
		//window.status = "測試哈哈"; //狀態列顯示文字
	}
	
	//模式窗體
	function testModalDialog(){
		//模式窗體(showModalDialog方法在谷歌瀏覽器中不支援,而且在谷歌瀏覽器中報錯,在火狐瀏覽器中執行沒問題,火狐瀏覽器支援該方法,其他瀏覽器沒測試,就測試了火狐和谷歌)
		window.showModalDialog("calculator.html");
	}
	
	function testClose(){
		window.close();
	}
	
	function testScreen(){
		//可以這樣理解,screen也是一個內建物件,screen物件是window物件內的一個屬性,window一般可以省略不寫,所以可以直接寫成screen.height
		alert("螢幕大小 = " + window.screen.width + " " + screen.height);
		if (screen.width != 1024 || screen.height != 768 ) {
			alert("請把螢幕設定成" + screen.width + " " + screen.height);
		}
	}
	
	function testHistory(){
		//history.back();
		//history.forward();
		//正數 1 就是向前(前進)即forward(),負數 -1就是向後(後退)即back()
		//history.go(-1);
	}
	
	function testLocation(){
		location.href = "calculator.html";
		//window.location.href = "calculator.html";
		//window.location = "calculator.html";
		//location = "calculator.html";
	}
	
</script>
</head>
<body>
														 
<input type="button" value="javascript程式碼直接寫在事件中測試1" onclick="javascript:alert('測試哈哈哈')">
<input type="button" value="javascript程式碼直接寫在事件中測試2" onclick="alert('測試呵呵呵')">
<input type="button" value="測試window物件的知識點" onclick="testWindow()">
<input type="button" value="測試window.alert方法" onclick="testAlert()">
<input type="button" value="測試window.confirm方法" onclick="testConfirm()">
<input type="button" value="測試window.open方法" onclick="testOpen()">
<input type="button" value="測試瀏覽器狀態列" onclick="testStatus()">
<input type="button" value="測試模式窗體" onclick="testModalDialog()">
<input type="button" value="測試關閉窗體,測試close方法" onclick="testClose()">
<input type="button" value="測試螢幕" onclick="testScreen()">
<input type="button" value="測試location" onclick="testLocation()">

</body>
</html>