JS那些事儿(6)-BOM

来源:互联网 发布:sony xz premium 知乎 编辑:程序博客网 时间:2024/06/15 00:23

1
浏览器要显示一个网页,不光跟网页的内容相关。还有浏览器窗口的大小、位置,浏览器弹出的警告框、提示框,还有cookie,有的浏览器客户端还有定时器(比如定时刷新图片)。

2
其实JavaScript语言已经将整个浏览器窗口封装为window对象,这个封装行为就是BOM(browser object model)——浏览器对象模型。

3
document其实也就是window.document,文档也是浏览器窗口对象的一个属性而已。

4
可以打开新窗口、对话框:

window.open("a.html");//打开新窗口window.alert("sometext");//提示框window.confirm("sometext");//确认框var input=window.prompt("sometext","defaultvalue");//文本输入框

5
window.location代表跟网页位置相关的信息

window.location.hostname web域名window.location.pathname 当前页面的路径和文件名window.location.port web端口window.location.protocol 使用的web协议window.location.href 当前页面的 URL。

6
还可以通过window对象设置定时器

//设置定时器var inter=window.setInterval(function(){alert("Hello")},3000);//清除定时器clearInterval(inter);//任务var time=window.setTimeout(function(){alert("Hello")},3000);//清除任务window.clearTimeout(time);
原创粉丝点击