JS中常用的全局屬性及方法
window對(duì)象
window對(duì)象表示當(dāng)前的瀏覽器窗口。
window對(duì)象的成員屬性就是全局屬性。
window對(duì)象的成員方法就是全局方法。
全局屬性
JSON
document Document對(duì)象
console Console
screen Screen
location Location
history History
navigator Navigator
Storage
localStorage HTML5 本地存儲(chǔ)
sessionStorage
closed 是否關(guān)閉
parent 父窗口
self 當(dāng)前窗口自己,等價(jià)于window。
innerHeight 文檔顯示區(qū)的高度
innerWidth 寬度
outerHeight 窗口的外部高度(包含工具條和滾動(dòng)條)
outerWidth 外部寬度
全局方法
三種彈出框
alert(?message) 警告框。無(wú)返回值|返回值為undefined。
confirm(?message) 確認(rèn)框。返回值為boolean類(lèi)型(true或false)。
prompt(?message, ?defaultValue) 提示框。返回值為string類(lèi)型或?yàn)閚ull。
定時(shí)
setTimeout(handler, ?timeout, ...arguments) 設(shè)置超時(shí)時(shí)間
clearTimeout(?handle)
setInterval(handler, ?timeout, ...arguments) 設(shè)置間隔時(shí)間
clearInterval(?handle)
字符串編碼及解碼
escape(html) 對(duì)HTML代碼|漢字進(jìn)行編碼(編碼為Unicode,格式:\uxxxx)
unescape(string) 對(duì)Unicode字符串進(jìn)行解碼|對(duì)漢字直接返回。
window.escape("你好")
"%u4F60%u597D"
window.unescape("%u4F60%u597D")
"你好"
window.unescape("你好")
"你好"
Base64編碼及解碼
btoa(asciiStr) 對(duì)傳入的英文字符串進(jìn)行Base64編碼。
atob(base64Str) 對(duì)經(jīng)過(guò)Base64編碼的字符串進(jìn)行解碼,信息還原。
btoa("Hello World")
"SGVsbG8gV29ybGQ="
atob("SGVsbG8gV29ybGQ=")
"Hello World"
URI編碼及解碼
encodeURI(uri) 編碼URI。
decodeURI(encodedURI) 解碼經(jīng)過(guò)編碼的URI。
encodeURIComponent(uriComponent) 對(duì)uri組件進(jìn)行編碼
decodeURIComponent(encodedURIComponent) 對(duì)經(jīng)過(guò)編碼的url組件進(jìn)行組件解碼。
window.encodeURI("https://www.baidu.com/s?wd=張三")
"https://www.baidu.com/s?wd=%E5%BC%A0%E4%B8%89"
window.decodeURI("https://www.baidu.com/s?wd=%E5%BC%A0%E4%B8%89")
"https://www.baidu.com/s?wd=張三"
encodeURIComponent("https://www.baidu.com/s?wd=張三")
"https%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3D%E5%BC%A0%E4%B8%89"
decodeURIComponent("https%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3D%E5%BC%A0%E4%B8%89")
"https://www.baidu.com/s?wd=張三"
類(lèi)型判斷
window.isBlank(str) 判斷str是否為空白(空格,回車(chē) 換行等)
window.isFinite(number) 判斷number是否為有限的(若傳入string會(huì)先自動(dòng)轉(zhuǎn)換成number格式,再判斷。NaN直接返回false,不是有限的數(shù)值)
window.isNaN(number) 判斷number是否不是數(shù)值。(boolean和數(shù)值都會(huì)返回false,因?yàn)樗麄兪菙?shù)值)。
類(lèi)型轉(zhuǎn)換
window.parseInt(string)
window.parseFloat(string)
window.Number() 構(gòu)造方法。會(huì)對(duì)傳入的變量類(lèi)型進(jìn)行自動(dòng)轉(zhuǎn)換。
window.String()
window.Boolean()
window.Symbol()
window.Date()
window.RegExp()
window.Array();
window.Object()
其他
window.eval(str) 對(duì)字符串中的JS表達(dá)式進(jìn)行重新運(yùn)算,求出運(yùn)算后的內(nèi)容。
窗口相關(guān)
stop() 停止頁(yè)面載入
open(?url, ?target, ?features)
close()
resizeTo(x, y)
moveTo(x, y)