瀏覽器對(duì)象模型 (BOM) 使 JavaScript 有能力與瀏覽器“對(duì)話”。
window
- 所有的瀏覽器都支持windou對(duì)象,他表示瀏覽器的窗口
- 所有 JavaScript 全局對(duì)象、函數(shù)以及變量均自動(dòng)成為 window 對(duì)象的成員。
- 全局變量是 window 對(duì)象的屬性。
- 全局函數(shù)是 window 對(duì)象的方法。
- HTML DOM 的 document 也是 window 對(duì)象的屬性之一
下面的代碼顯示瀏覽器窗口的高度和寬度,不包括工具欄/滾動(dòng)條。
var width =window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var height =window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
另外一些操作
window.open() - 打開(kāi)新窗口
window.close()- 關(guān)閉當(dāng)前窗口
window.moveTo() - 移動(dòng)當(dāng)前窗口
window.resizeTo()- 調(diào)整當(dāng)前窗口的尺寸
Screen
screen.availWidth - 可用的屏幕寬度
screen.availHeight - 可用的屏幕高度
Location
window.location 對(duì)象用于獲得當(dāng)前頁(yè)面的地址 (URL),并把瀏覽器重定向到新的頁(yè)面。
location.hostname 返回 web 主機(jī)的域名
location.pathname 返回當(dāng)前頁(yè)面的路徑和文件名
location.port 返回 web 主機(jī)的端口 (80 或 443)
location.protocol 返回所使用的 web 協(xié)議(http:// 或 https://)
location.href 返回當(dāng)前頁(yè)面的 URL
location.pathname 返回 URL 的路徑名
location.assign() 這個(gè)方法加載新的文檔
History
window.history 對(duì)象在編寫時(shí)可不使用 window 這個(gè)前綴。
history.back() - 與在瀏覽器點(diǎn)擊后退按鈕相同
history.forward() - 與在瀏覽器中點(diǎn)擊按鈕向前相同
Navigator
window.navigator 對(duì)象包含有關(guān)訪問(wèn)者瀏覽器的信息,window.navigator 對(duì)象在編寫時(shí)可不使用 window 這個(gè)前綴。
警告:來(lái)自 navigator 對(duì)象的信息具有誤導(dǎo)性,不應(yīng)該被用于檢測(cè)瀏覽器版本,這是因?yàn)椋?br>
navigator 數(shù)據(jù)可被瀏覽器使用者更改
瀏覽器無(wú)法報(bào)告晚于瀏覽器發(fā)布的新操作系統(tǒng)
消息框
alert 是警示框、可以使用“\n”來(lái)使警示語(yǔ)進(jìn)行折行。
confirm 是確認(rèn)框,用于使用戶可以驗(yàn)證或者接受某些信息。
prompt("文本","默認(rèn)值") 是提示框, 第二個(gè)引號(hào)中的內(nèi)容是默認(rèn)值 也就是占位語(yǔ)。
計(jì)時(shí)事件
計(jì)時(shí)時(shí)間的作用就是我們可以使用js來(lái)設(shè)定一個(gè)經(jīng)過(guò)一段時(shí)間之后才執(zhí)行的事件,而不是調(diào)用的時(shí)候就直接執(zhí)行。
在JavaScritp 中使用計(jì)時(shí)事件有兩個(gè)關(guān)鍵方法
setTimeout("js語(yǔ)句",ms)未來(lái)的某時(shí)執(zhí)行代碼,第一個(gè)參數(shù)是要執(zhí)行的js代碼,第二個(gè)參數(shù)是以毫秒為單位的時(shí)間。
注:1000ms = 1s。clearTimeout()取消setTimeout()
取消計(jì)時(shí)時(shí)間的寫法為:
var t = setTimeout("alert("哈哈")",5000);
clearTimeout(t);
Cookies
cookie 用來(lái)識(shí)別用戶。cookie 是存儲(chǔ)于訪問(wèn)者的計(jì)算機(jī)中的變量。每當(dāng)同一臺(tái)計(jì)算機(jī)通過(guò)瀏覽器請(qǐng)求某個(gè)頁(yè)面時(shí),就會(huì)發(fā)送這個(gè) cookie。你可以使用 JavaScript 來(lái)創(chuàng)建和取回 cookie 的值。
<html>
<head>
<script type="text/javascript">
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=")
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(";",c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return ""
}
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : "; expires="+exdate.toGMTString())
}
function checkCookie()
{
username=getCookie('username')
if (username!=null && username!="")
{alert('Welcome again '+username+'!')}
else
{
username=prompt('Please enter your name:',"")
if (username!=null && username!="")
{
setCookie('username',username,365)
}
}
}
</script>
</head>
本文內(nèi)容學(xué)習(xí)自W3School,侵刪。。。