BOM相关方法和属性
发布日期:2021-05-06 19:35:34 浏览次数:21 分类:技术文章

本文共 1418 字,大约阅读时间需要 4 分钟。

1. BOM

1)打开,关闭窗口

open()方法,打开窗口

open(页面的地址url,打开的方式) 方法 打开一个新的窗口(页面)

如果 url 为空,则默认打开一个空白页面
如果打开方式为空,默认为新窗口方式打开
返回值:返回新打开的窗口的 window 对象

close()方法 关闭窗口

window.close();

1. ff : 无法关闭
2. chrome : 直接关闭
3. ie : 询问用户
但是通过以下方法,可以关闭在本窗口中通过js方法打开的新窗口
opener = window.open();
opener.close();

2)window下面常见的属性

window.navigator.userAgent -> 浏览器信息

通过这个方式可以判断浏览器的类别

alert( window.navigator.userAgent )if ( window.navigator.userAgent.indexOf('MSIE') != -1 ) {    alert('我是ie');} else {    alert('我不是ie');}

window.location : 浏览器地址信息

window.location下面还有属性

//window.location.href:urlalert( window.location.href );//window.location.search:url?后面的内容alert( window.location.search );//window.location.hash:url#后面的内容alert( window.location.hash );

2. 文档宽高及窗口事件

可视区的尺寸(用户能看到的部分,窗口缩小宽高也变小)

document.documentElement.clientWidth
document.documentElement.clientHeight

滚动距离 (滚动距离是可视区的顶部到整个页面的顶部的距离)如下所示大的是整个页面 小的是可视区

document.body.scrollTop[scrollLeft] //chrome用这个
document.documentElement.scrollTop[scrollLeft]//其他浏览器用这个
这里写图片描述
注意:为了处理兼容,可以通过下面这种方式写
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop

内容高度

document.documentElement.clientHeight
如下所示,包含padding,margin
这里写图片描述

文档高度 包含padding,margin

document.body.offsetHeight
document.documentElement.offsetHeight

onscroll : 当滚动条滚动的时候触发

onresize : 当窗口大小发生改变的时候出发

var i = 0;window.onscroll = function() {
document.title = i++;}window.onresize = function() {
document.title = i++;}
上一篇:焦点事件
下一篇:字符串详解

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年03月21日 14时40分02秒