美文网首页
BOM(浏览器对象模型Browser Object Modal)

BOM(浏览器对象模型Browser Object Modal)

作者: paradisefj | 来源:发表于2020-03-19 19:21 被阅读0次

BOM的核心对象是windows,他表示浏览器的一个实例。在浏览器中,window对象具有双重角色,它既是通过JavaScript访问浏览器窗口的一个接口,又是ECMAScript规定的Global对象。

本文主要介绍4个BOM对象:

  • window

  • location

  • navigator

  • screen

  • history

1. window 对象

1.1 全局作用域

由于window对象同时是ECMAScript中的Global对象,因此所有在全局作用域中声明的变量、函数都会变成window对象的属性和方法。

1.2 窗口关系及框架

如果页面包含框架,则每个框架都拥有自己的window对象,并且保存在frames集合中。

由于HTML5已经不支持 frame 标签,再此不对框架中的window对象进行深入的讨论。

1.3 窗口大小

在不同的浏览器中,没有统一的属性能获取到浏览器窗口的大小。但是可以通过一些方法获取浏览器视口的大小。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n21" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">// 获取浏览器视口大小
var pageWidth = window.innerWidth;
var pageHeight = window.innerHeight;
if(typeof pageWidth != "number") {
if(document.compatMode == "CSS1Compat") {
pageWidth = document.documentElement.clientWidth;
pageHeight = document.documentElement.clientHeight;
} else {
pageWidth = document.body.clientWidth;
pageHeight = document.body.clientHeight;
}
}</pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n24" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">window.moveTO(0,0); // 移动到左上角
window.moveTo(100, 100); // 移动到(100,100)
window.moveBy(0, 100); // 向下移动100px
window.moveBy(-50, 0); // 向左移动50px</pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n95" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; background-position: inherit inherit; background-repeat: inherit inherit;">if(history.length == 0) {
// 这是用户打开窗口后的第一个页面
}</pre>

history还有一个length属性保存着历史记录的数量。

history.forward() 等同于 history.go(1)

history.back() 等同于 history.go(-1)

history还提供了back()forward()简写后退和前进。

history.go("wrox.com")

当参数为字符串时,浏览器会跳转到历史记录中包含该字符串的第一个位置。如果历史记录中不包含该字符串,则什么也不做。

history.go(1);

history.go(-1);

使用go()方法可以在历史记录中任意跳转。方法接受一个参数,当参数为数字时,表示向后或者向前跳转的页面树的一个整数值。负数表示向后跳转(类似浏览器“后退”按钮),正数表示向前跳转(类似浏览器“前进”按钮)。

history对象中保存着用户上网的历史记录。由于安全原因,通过代码无法获取用户浏览过的URL。

5. history 对象

每个浏览器中的screen对象都包含不同的属性。

screen对象基本上只用来表名客户端的能力,其中包括浏览器窗口外部的显示器的信息。

4. screen 对象

  1. navigator对象用来识别客户端浏览器。

  2. 非IE浏览器中,可以使用navigator.plugins来识别插件。

  3. 使用navigator.registerContentHandler()navigator.registerProtocalHandler()来注册处理程序。

3. navigator 对象

location.replace()接受一个参数url,导航到相应的页面。虽然会导致浏览器的位置发生变化,但不会再历史记录中生成新记录。在调用该方法后,用户不能回到前一个页面。

上述方式修改URL后,浏览器的历史记录中就会生成一条新的记录。

注:每次修改location的属性(hash除外),页面都会以新URL重新加载。

location.hash = '#section1; // 在上面的基础上,url修改为http://www.wrox.com#section1'

location.href = "http://www.wrox.com"

window.location = "http://www.wrox.com"

location.assign("http://www.wrox.com")

可以通过location.assign()传递一个url来打开新的url,并且浏览器的历史记录中会生成一条记录。还可以通过修改location的属性来改变url。

2.2 位置操作

  • hash

  • host

  • hostname

  • href

  • pathname

  • port: url中指定的端口,若url中不包含端口,则返回空字符串

  • protocal: "http:"、"https"

  • search: 返回url的查询字符串,这个字符串以问号开头

2.1 对象属性

location对象提供了当前窗口中加载的文档有关的信息,还提供了一些导航功能。location对象既是window对象的属性,也是document对象的属性。window.location === document.location // true

2. location 对象

  1. 超时调用 setTimeout()

  2. 间歇调用 setInterval()

  3. window.open()

  4. 调整窗口大小,绝对大小:window.resizeTo()

  5. 调整窗口大小,相对大小:resizeBy()

  6. altert()

  7. confirm()

  8. prompt()

1.4 window中的几个重要函数

无法再跨浏览器的条件下取得窗口的左边和上边的精确坐标值。但可以通过moveTo()moveBy()有可能将窗口精确移动到同一个新位置。

1.4 窗口位置

相关文章

  • BOM

    BOM 浏览器对象模型 BOM(Browser Object Model)是指浏览器对象模型,浏览器对象模型提供来...

  • BOM模型

    BOM模型 浏览器对象模型(Browser Object Model),BOM对象是JavaScript的核心,该...

  • BOM概述

    BOM 概述 1. BOM(浏览器模型) Browser Object Moder浏览器对象模型 什么是BOM ?...

  • JavaScript基础知识点--BOM基础及window对象

    什么是 BOM BOM(browser object model)浏览器对象模型 BOM 对象 window na...

  • DOM和BOM

    BOM 介绍 BOM:browser object model浏览器对象模型,用对象操作浏览器 window对象是...

  • 复习笔记之API(9) BOM浏览器对象模型

    BOM浏览器对象模型 BOM(Browser Object Module)概述:浏览器对象模型,它提供了独立于内容...

  • JavaScript函数7:BOM

    BOM(browser object model) 浏览器对象模型 window === 浏览器 ...

  • BOM和DOM编程

    BOM(browser object model):浏览器对象模型 浏览器:windows对象 Window 对象...

  • BOM

    BOM(Browser Object Model)浏览器对象模型。BOM具有很多的对象window,navigat...

  • JavaScript--BOM

    JavaScript--BOM BOM(browser object model)浏览器对象模型 一、window...

网友评论

      本文标题:BOM(浏览器对象模型Browser Object Modal)

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