美文网首页
JS中常用的全局属性及方法

JS中常用的全局属性及方法

作者: 吃肉肉不吃肉肉 | 来源:发表于2020-02-25 20:56 被阅读0次

    JS中常用的全局属性及方法

    window对象
    window对象表示当前的浏览器窗口。
    window对象的成员属性就是全局属性。
    window对象的成员方法就是全局方法。

    全局属性

    JSON
    document Document对象
    console Console
    screen Screen
    location Location
    history History
    navigator Navigator

    Storage
    localStorage HTML5 本地存储
    sessionStorage

    closed 是否关闭
    parent 父窗口
    self 当前窗口自己,等价于window。

    innerHeight 文档显示区的高度
    innerWidth 宽度
    outerHeight 窗口的外部高度(包含工具条和滚动条)
    outerWidth 外部宽度

    全局方法

    三种弹出框
    alert(?message) 警告框。无返回值|返回值为undefined。
    confirm(?message) 确认框。返回值为boolean类型(true或false)。
    prompt(?message, ?defaultValue) 提示框。返回值为string类型或为null。

    定时

    setTimeout(handler, ?timeout, ...arguments) 设置超时时间
    clearTimeout(?handle)
    setInterval(handler, ?timeout, ...arguments) 设置间隔时间
    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) 对经过Base64编码的字符串进行解码,信息还原。
    btoa("Hello World")
    "SGVsbG8gV29ybGQ="
    atob("SGVsbG8gV29ybGQ=")
    "Hello World"

    URI编码及解码

    encodeURI(uri) 编码URI。
    decodeURI(encodedURI) 解码经过编码的URI。
    encodeURIComponent(uriComponent) 对uri组件进行编码
    decodeURIComponent(encodedURIComponent) 对经过编码的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会先自动转换成number格式,再判断。NaN直接返回false,不是有限的数值)
    window.isNaN(number) 判断number是否不是数值。(boolean和数值都会返回false,因为他们是数值)。

    类型转换

    window.parseInt(string)
    window.parseFloat(string)

    window.Number() 构造方法。会对传入的变量类型进行自动转换。
    window.String()
    window.Boolean()

    window.Symbol()

    window.Date()
    window.RegExp()
    window.Array();
    window.Object()

    其他

    window.eval(str) 对字符串中的JS表达式进行重新运算,求出运算后的内容。

    窗口相关

    stop() 停止页面载入
    open(?url, ?target, ?features)
    close()
    resizeTo(x, y)
    moveTo(x, y)

    相关文章

      网友评论

          本文标题:JS中常用的全局属性及方法

          本文链接:https://www.haomeiwen.com/subject/cukqchtx.html