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

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 窗口位置

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,635评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,628评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,971评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,986评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,006评论 6 394
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,784评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,475评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,364评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,860评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,008评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,152评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,829评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,490评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,035评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,428评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,127评论 2 356

推荐阅读更多精彩内容