JS中常用的全局屬性及方法
window對象
window對象表示當(dāng)前的瀏覽器窗口悉默。
window對象的成員屬性就是全局屬性。
window對象的成員方法就是全局方法肉迫。
全局屬性
JSON
document Document對象
console Console
screen Screen
location Location
history History
navigator Navigator
Storage
localStorage HTML5 本地存儲
sessionStorage
closed 是否關(guān)閉
parent 父窗口
self 當(dāng)前窗口自己验辞,等價于window。
innerHeight 文檔顯示區(qū)的高度
innerWidth 寬度
outerHeight 窗口的外部高度(包含工具條和滾動條)
outerWidth 外部寬度
全局方法
三種彈出框
alert(?message) 警告框喊衫。無返回值|返回值為undefined跌造。
confirm(?message) 確認框。返回值為boolean類型(true或false)族购。
prompt(?message, ?defaultValue) 提示框壳贪。返回值為string類型或為null。
定時
setTimeout(handler, ?timeout, ...arguments) 設(shè)置超時時間
clearTimeout(?handle)
setInterval(handler, ?timeout, ...arguments) 設(shè)置間隔時間
clearInterval(?handle)
字符串編碼及解碼
escape(html) 對HTML代碼|漢字進行編碼(編碼為Unicode寝杖,格式:\uxxxx)
unescape(string) 對Unicode字符串進行解碼|對漢字直接返回违施。
window.escape("你好")
"%u4F60%u597D"
window.unescape("%u4F60%u597D")
"你好"
window.unescape("你好")
"你好"
Base64編碼及解碼
btoa(asciiStr) 對傳入的英文字符串進行Base64編碼。
atob(base64Str) 對經(jīng)過Base64編碼的字符串進行解碼瑟幕,信息還原磕蒲。
btoa("Hello World")
"SGVsbG8gV29ybGQ="
atob("SGVsbG8gV29ybGQ=")
"Hello World"
URI編碼及解碼
encodeURI(uri) 編碼URI。
decodeURI(encodedURI) 解碼經(jīng)過編碼的URI只盹。
encodeURIComponent(uriComponent) 對uri組件進行編碼
decodeURIComponent(encodedURIComponent) 對經(jīng)過編碼的url組件進行組件解碼辣往。
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=張三"
類型判斷
window.isBlank(str) 判斷str是否為空白(空格,回車 換行等)
window.isFinite(number) 判斷number是否為有限的(若傳入string會先自動轉(zhuǎn)換成number格式殖卑,再判斷站削。NaN直接返回false,不是有限的數(shù)值)
window.isNaN(number) 判斷number是否不是數(shù)值孵稽。(boolean和數(shù)值都會返回false钻哩,因為他們是數(shù)值)屹堰。
類型轉(zhuǎn)換
window.parseInt(string)
window.parseFloat(string)
window.Number() 構(gòu)造方法。會對傳入的變量類型進行自動轉(zhuǎn)換街氢。
window.String()
window.Boolean()
window.Symbol()
window.Date()
window.RegExp()
window.Array();
window.Object()
其他
window.eval(str) 對字符串中的JS表達式進行重新運算扯键,求出運算后的內(nèi)容。
窗口相關(guān)
stop() 停止頁面載入
open(?url, ?target, ?features)
close()
resizeTo(x, y)
moveTo(x, y)