HTML DOM 之 BOM对象:Browser Object Model (浏览器对象模型.)

JavaScript Window - 浏览器对象模型(BOM:Browser Object Model)

浏览器对象模型 (BOM) 使 JavaScript 有能力与浏览器“对话”。
浏览器对象模型(BOM:Browser Object Model)尚无正式标准。
由于现代浏览器已经(几乎)实现了 JavaScript 交互性方面的相同方法和属性,因此常被认为是 BOM 的方法和属性。
对象:
    1、Window、
    2、Window Screen、
    3、Window Location、
    4、Window History、
    5、Window Navigator、
    6、JavaScript 消息框、
    7、JavaScript 计时、
    8、JavaScript Cookies

1、Window 对象
    所有浏览器都支持 window 对象。它表示浏览器窗口。
    所有 JavaScript 全局对象、函数以及变量均自动成为 window 对象的成员。
    全局变量是 window 对象的属性,全局函数是 window 对象的方法。
    甚至 HTML DOM 的 document 也是 window 对象的属性之一:
        window.document.getElementById("header");
        与此相同:
        document.getElementById("header");
    Window 尺寸
        有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。
            对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:
                •window.innerHeight - 浏览器窗口的内部高度
                •window.innerWidth - 浏览器窗口的内部宽度
            对于 Internet Explorer 8、7、6、5:
                •document.documentElement.clientHeight
                •document.documentElement.clientWidth
            或者
                •document.body.clientHeight
                •document.body.clientWidth
        实用的 JavaScript 方案(涵盖所有浏览器):
            实例
                var w=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
                var h=window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
            alert("浏览器的内部窗口宽度:"+ w +",高度:"+ h ");
        一些其他方法:
            •window.open() - 打开新窗口
            •window.close() - 关闭当前窗口
            •window.moveTo() - 移动当前窗口
            •window.resizeTo() - 调整当前窗口的尺寸

2、Window Screen
    window.screen 对象包含有关用户屏幕的信息。
    window.screen 对象在编写时可以不使用 window 这个前缀。
    可用宽度
    screen.availWidth 属性返回访问者屏幕的宽度,以像素计,减去界面特性,比如窗口任务栏。
    可用高度
    screen.availHeight 属性返回访问者屏幕的高度,以像素计,减去界面特性,比如窗口任务栏。

3、Window Location
    window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。
    window.location 对象在编写时可不使用 window 这个前缀。
    一些例子:
        属性:
            •location.hostname 返回 web 主机的域名
            •location.pathname 返回当前页面的路径和文件名
            •location.port 返回 web 主机的端口 (80 或 443)
            •location.protocol 返回所使用的 web 协议(http:// 或 https://)
            •location.href 属性返回当前页面的 URL。
        方法:
            location.assign("www.baidu.com") 方法加载新的文档。

4、Window History
    window.history 对象包含浏览器的历史。
    window.history 对象在编写时可不使用 window 这个前缀。
        为了保护用户隐私,对 JavaScript 访问该对象的方法做出了限制。
    一些方法:
        •history.back() - 与在浏览器点击后退按钮相同
        •history.forward() - 与在浏览器中点击按钮向前相同

5、Window Navigator
    window.navigator 对象包含有关访问者浏览器的信息。
    window.navigator 对象在编写时可不使用 window 这个前缀。
    属性:
        Browser CodeName: navigator.appCodeName ;
        Browser Name: navigator.appName ; //保存浏览器类型
        Browser Version: navigator.appVersion ; //存有浏览器的版本信息(其他信息中的一项)
        Cookies Enabled: navigator.cookieEnabled ;
        Platform: navigator.platform ;
        User-agent header: navigator.userAgent ;
        User-agent language: navigator.systemLanguage ;
    警告:来自 navigator 对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:
            •navigator 数据可被浏览器使用者更改
            •浏览器无法报告晚于浏览器发布的新操作系统

6、JavaScript 消息框
    6.1、警告框
        alert("文本");
        带有折行的警告框
        alert("再次向您问好!在这里,我们向您演示" + '\n' + "如何向警告框添加折行。");
    6.2、确认框
        confirm("文本");
        var r=confirm("确定删除吗?");
        if (r==true){
            alert("你点了确定!");
        }else{
            alert("你点了取消!");
        }
    6.3、提示框
        prompt("文本","默认值");
        var name=prompt("请输入您的名字","Bill Gates(默认值)");
        if (name!=null && name!=""){
            document.write("你好!" + name + " 今天过得怎么样?");
        }

7、JavaScript 计时
    通过使用 JavaScript,我们有能力做到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行。我们称之为计时事件。
        在 JavaScritp 中使用计时事件是很容易的,两个关键方法是:
        setInterval();每隔设定的时间执行一次代码
            clearInterval();取消setInterval()
        setTimeout();  未来的某时执行代码
            clearTimeout();  取消setTimeout()
    7.1、setInterval()
        语法: var tt = setInterval(javascript语句或函数,毫秒);
            每1秒(1000毫秒)在控制台打印aa,直到使用clearInterval停止
            tt.clearInterval();
    7.2、setTimeout()
        语法: var tt = setTimeout(javascript语句或函数,毫秒);
            setTimeout() 方法会返回某个值。在上面的语句中,值被储存在名为 t 的变量中,
                假如你希望取消这个 setTimeout(),你可以使用这个变量名来指定它。tt.clearTimeout();
            setTimeout() 的第一个参数是含有 JavaScript 语句的字符串。
                这个语句可能诸如 "alert('5 seconds!')",或者对函数的调用,诸如 alertMsg()"。

8、JavaScript Cookies
    cookie 用来识别用户。
    什么是cookie?
    cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。
    你可以使用 JavaScript 来创建和取回 cookie 的值。


猜你喜欢

转载自blog.csdn.net/weixin_42472048/article/details/80769836