JavaScript指令碼語言是一門基於物件導向的程式設計語言, 它也支援一些預定義對象支援的簡單物件模型.
JavaScript的對象由屬性和方法兩個基本元素組成.
Navigator對象
Navigator 對象管理著瀏覽器的基本資料, 如版本號碼, 作業系統等.
appName 顯示瀏覽器名稱
appVersion 瀏覽器版本號碼
platform 用戶端作業系統
onLine 瀏覽器是否線上
JavaEnabled() 是否啟用Javanavigator
<html>
<head>
<title>Navigator對象</title>
</head>
<body>
瀏覽器名稱:
<script>document.write(navigator.appName)</script><br>
瀏覽器版本:
<script>document.write(navigator.appVersion)</script><br>
作業系統:
<script>document.write(navigator.platform)</script><br>
線上情況:
<script>document.write(navigator.onLine)</script><br>
是否Java啟用:
<script>document.write(navigator.javaEnabled())</script><br>
</body>
</html>
Location對象:
Location對象是瀏覽器內建的一個靜態對象, 它顯示的是一個視窗對象所開啟的地址. 使用Location對象是要考慮許可權問題, 不同的協議或者不同的主機不能互相引用彼此的Location對象.
hostname 返回地址主機名稱
port 返回地址連接埠號碼
host 返回主機名稱和連接埠號碼<html>
<head>
<title>Location對象</title>
</head>
<body>
<script language="javascript">
document.write("地址主機名稱:");
document.write(location.hostname);
</script>
</body>
</html>
Window對象:
Window對象是一個優先順序很高的對象, Window對象包含了豐富的屬性,方法和其他時間驅動, 程式員可以簡單地操作這些簡單的屬性和方法, 對瀏覽器顯示視窗進行控制.
Window 對象的屬性:
self 當前視窗
parent 主視窗
top 頂部視窗
status 瀏覽器狀態列
Window 對象方法:
open() 開啟
close() 關閉
alert() 訊息框
confirm() 確認框
prompt() 提示框Window
<html>
<head>
<title>Window對象</title>
</head>
<body>
彈出百度首頁(www.baidu.com)
<script language="javascript">
window.open("http://www.baidu.com","newwindow","height=200,width=300,top=50,left=50")
</script>
</body>
</html>
Document對象:
JavaScript的輸入和輸出都必須通過對象來完成, Document就是輸出對象的其中之一, Document對象最主要的方法是write()<html>
<head>
<title>Document對象</title>
</head>
<body>
<script language="JavaScript">
document.write("<font color=red>今天你學JavaScript了嗎?</font>");
</script>
</body>
</html>
History對象:
在JavaScript指令碼語言中, History對象表示的是瀏覽曆史, 它包含了瀏覽器以前瀏覽過的網頁的網路地址.
Forward() 相當於瀏覽器工具列上的"前進"按鈕
Back() 相當於瀏覽器工具列上的"後退"按鈕
Go() 相當於瀏覽器工具列上的"轉到"按鈕History
<html>
<head>
<title>History對象</title>
</head>
<body>
<script language="JavaScript">
document.write("<font color=red>今天你學JavaScript了嗎?</font>");
</script>
<form>
<input name="前進" type="button" onclick="history.go(1)" value="前進">
<input name="後退" type="button" onclick="history.go(-1)" value="後退">
<input name="轉到" type="button" onclick="history.go(2)" value="轉到">
</form>
</body>
</html>
內建對象和方法:
JavaScript語言提供的內建對象的屬性和方法與其他物件導向程式設計語言的調用方式相同:
對象名.屬性名稱
對象名.方法名稱(參數)
Date 對象:getDate, getDay, getHour, getMouth, getSeconds, setDay,setHour, setMouth, setSeconds
String 對象: indexOF(), charAT(), toLowerCase(), toUpCase(), substring()
Math 對象: abs(), acos(), atan(), max(), min(), sprt()
Array 對象: 定義數組
使用者還可以根據需要,自訂對象.