• window&navigator&screen&location


    window

    window对象不但充当全局作用域,而且表示浏览器窗口。

    window对象有innerWidthinnerHeight属性,可以获取浏览器窗口的内部宽度和高度。内部宽高是指除去菜单栏、工具栏、边框等占位元素后,用于显示网页的净宽高。

    对应的,还有一个outerWidthouterHeight属性,可以获取浏览器窗口的整个宽高。

    navigator对象表示浏览器的信息,最常用的属性包括:

    • navigator.appName:浏览器名称;
    • navigator.appVersion:浏览器版本;
    • navigator.language:浏览器设置的语言;
    • navigator.platform:操作系统类型;
    • navigator.userAgent:浏览器设定的User-Agent字符串。

    请注意navigator的信息可以很容易地被用户修改,所以JavaScript读取的值不一定是正确的。很多初学者为了针对不同浏览器编写不同的代码,喜欢用if判断浏览器版本,例如:

    var width;
    if (getIEVersion(navigator.userAgent) < 9) {
        width = document.body.clientWidth;
    } else {
        width = window.innerWidth;
    }
    

    但这样既可能判断不准确,也很难维护代码。正确的方法是充分利用JavaScript对不存在属性返回undefined的特性,直接用短路运算符||计算:

    var width = window.innerWidth || document.body.clientWidth;
    

    screen

    screen对象表示屏幕的信息,常用的属性有:

    • screen.width:屏幕宽度,以像素为单位;
    • screen.height:屏幕高度,以像素为单位;
    • screen.colorDepth:返回颜色位数,如8、16、24。

    location

    location对象表示当前页面的URL信息。例如,一个完整的URL:

    http://www.example.com:8080/path/index.html?a=1&b=2#TOP
    

    可以用location.href获取。要获得URL各个部分的值,可以这么写:

    location.protocol; // 'http'
    location.host; // 'www.example.com'
    location.port; // '8080'
    location.pathname; // '/path/index.html'
    location.search; // '?a=1&b=2'
    location.hash; // 'TOP'
    

    要加载一个新页面,可以调用location.assign()。如果要重新加载当前页面,调用location.reload()方法非常方便。

  • 相关阅读:
    CR655 题解
    International Master 计划试题乱做 part1
    CR604 题解
    用js做分页,点击下一页时,直接跳到了最后一页——Number()的妙用
    RabbitMQ生产者消费者模型(二)
    线程的状态(二)
    Python服务端测试开发训练营
    RabbitMQ环境搭建(一)
    RabbitMQ死信队列机制(七)
    RabbitMQ限流机制(五)
  • 原文地址:https://www.cnblogs.com/EganZhang/p/6538518.html
Copyright © 2020-2023  润新知