JavaScript:BOM(浏览器对象)

BOM(Browser Object Model)

只要和浏览器打交道,BOM都是需要了解的,虽然现在JavaScript可以在服务器端运行,但是前端主要语言还是JavaScript。之前的文章写道浏览器环境下,this指向是window,那么window是个啥东西呢?往下看吧。

1、window

<html>
    <script type="text/javascript">
        console.log(window);
    </script>
</html>

在浏览器中F12–>console栏下会有如下的结果:
在这里插入图片描述

里面所有内容都是window对象的属性和方法,有我们常见的alert、close、blur、document、focus、history、location、localStorage、onclick、onblur、onchange等等等等,我们队DOM操作的事件,属性都在window里。
因为全局this就是window所以可以省略window.alert中的window,其他也一样的道理。

常用的方法有:

open

用途:打开新窗口
语法:window.open(url)

close

用途:关闭当前浏览器窗口
语法:window.close()

alert和confirm

用途:弹出提示框
语法:window.alert(message); window.confirm(message)
区别:alert框只有确认按钮,confirm有确认和取消两个按钮

prompt

用途:弹出提示框,让用户输入东西,返回值是用户输入的内容
语法:window.prompt(title,msg);
参数:title是框名字,msg是用户输入框的默认值

setTimeout

用途:定时器
语法:window.setTimeout(expression,millisecond)
参数:millisecond是毫秒数,expression是要执行的代码
说明:这个方法执行一次,并不会每过millisecond毫秒执行一次expression

clearTimeout

用途:解除定时器
语法:window.clearTimeout(timer)
参数:timer是setTimeout的返回值

setInterval

用途:定时器(能循环执行)
语法:window.setInterval(expression,millisecond)
参数:millisecond是毫秒数,expression是要执行的代码
说明:每过millisecond毫秒执行一次expression

clearInterval

用途:解除定时器
语法:window.clearInterval(timer)
参数:timer是setInterval的返回值

扫描二维码关注公众号,回复: 3313176 查看本文章

back

用途:相当于用户点击浏览器的回退按钮
语法:window.back()

blur和focus

用途:blur是移出焦点 focus是获取焦点
语法:window.blur() window.focus()

常用的属性有:

document

用途:对文档DOM操作都需要用的属性

history

用途:历史记录方面的属性

location

用途:用来处理协议、url、uri,query参数以及页面跳转(location.href)等等

navigator

用途:获取浏览器信息,比如处理浏览器版本判断,有时候不同版本浏览器bug不同,需要单独处理

2、document对象

常用属性

document.title :设置文档标题,也就是html的标签
document.bgColor:设置页面的背景色
document.fgColor:设置文本颜色(前景色)
document.linkColor:未点击过的链接颜色
document.alinkColor:焦点在此链接上的颜色
document.vlinkColor:已点击过的链接颜色
document.URL:设置url属性,从而在同一个窗口打开另一网页
document.fileCreateDate:文件建立日期,只读属性
document.fileModifiedDate:文件修改日期,只读属性
document.fileSize:文件大小,只读属性
document.cookie:设置和读取cookie
document.charset:设置字符集
document.referrer:上一个页面的url

常用方法

document.write() //动态向页面写入内容
document.createElement(Tag) //创建一个html标签对象
document.getElementById(ID) //获得指定ID值的对象
document.getElementsByName(Name) //获得指定Name值的对象

3、location对象

常用属性

document.location.hash // #号后的部分
document.location.host // 域名+端口号
document.location.hostname // 域名
document.location.href // 完整URL
document.location.pathname // 目录部分
document.location.port // 端口号
document.location.protocol // 网络协议(http:)
document.location.search // ?号后的部分

常用方法:

documeny.location.reload() //刷新网页
document.location.reload(URL) //打开新的网页
document.location.assign(URL) //打开新的网页
document.location.replace(URL) //打开新的网页

猜你喜欢

转载自blog.csdn.net/tsfx051435adsl/article/details/82824445