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

3-1window

window是浏览器的一个实例,在浏览器中,window对象有双重角色,它既是通过Javascript访问浏览器窗口的一个接口,又是ECMAScript规定的Global对象(全局对象)。
所有的全局变量和全局方法都被归在window上

  1. var age =15;
  2. //声明一个全局变量
  3. window.name="chengchao";
  4. //相当于var name = "chengchao"

3-1-1Window对象的方法

A.window.alert()
功能:显示一个带有指定消息和包含确定和取消按钮的对话框

  1. <script>
  2. var t=window.confirm("要不要");
  3. console.log(t);
  4. </script>

B.window.confirm(“msg”)
功能:显示一个带有指定消息和包含确定和取消按钮的对话框。点击确定返回true,取消返回false;

  1. <ul>
  2. <li>html <button>删除</button></li>
  3. <li>js <button>删除</button></li>
  4. <li>vue<button>删除</button></li>
  5. </ul>
  6. <script>
  7. var btns=document.getElementsByTagName("button");
  8. for(var i=0;i<btns.length;i++){
  9. btns[i].onclick=function(){
  10. var temp=window.confirm("确定吗");
  11. if(temp){
  12. this.parentNode.style.display="none";
  13. }
  14. }
  15. }
  16. </script>

C.window.prompt(“text,defaultText”)

  1. 参数说明:
  2. text:在对话框中显示的文本
  3. defaultText:默认输入文本
  4. 返回值:点取消按钮,返回null
  5. 点确定按钮,返回输入的文本
  6. eg:
  7. var result=window.prompt("请输入你的星座","狮子座");
  8. console.log(result);
  1. <script>
  2. var test=window.prompt("请输入","hello");
  3. console.log(test);
  4. </script>

D.window对象方法——定时器

1.超时调用-setTimeout()间隔一段时间只会触发一次

  1. <script>
  2. // 超时调用
  3. //间隔一段时间只会触发一次
  4. setTimeout(function(){
  5. console.log("hello")
  6. },2000)
  7. </script>

setTimeout() 持续触发事件的方法(递归)

  1. function show(){
  2. setTimeout(function(){
  3. console.log("123");
  4. show();
  5. },1000)
  6. }
  7. show();

2.间歇调用-setInterval()每间隔一段时间,就会触发事件

  1. 间歇调用,每间隔一段时间,就会触发事件
  2. */
  3. setInterval(function(){
  4. console.log("world")
  5. },2000)

3.清除定时clearInterval()

  1. <button id="btn">停止</button>
  2. <script>
  3. var btn=document.getElementById("btn");
  4. var temp=setInterval(function(){
  5. console.log("hello");
  6. },1000)
  7. btn.onclick=function(){
  8. clearInterval(temp);
  9. }
  10. </script>

3-2location对象

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

  1. location.hrefwindow.location.href等价
  2. 语法:location.hash
  3. 功能:返回URL中的hash(#号后跟0或多个字符),如果不包含则返回空字符串
  4. 语法:location.host
  5. 功能:返回服务器名称和端口号
  6. 语法:location.hostname
  7. 功能:返回不带端口号的服务器名称
  8. 语法:location.pathname
  9. 功能:返回URL中的目录和(或)文件名
  10. 语法:location.port
  11. 功能:返回URL中指定的端口号,如果没有,返回空字符串

3-3.history对象


  1. history对象保存了用户在浏览器中访问页面的历史记录
  2. 语法:history.back()
  3. 功能:回到历史记录的上一步
  4. 相当于是用了history.go(-1)
  5. //-1表示前一步,-2前两部
  6. 语法:history.go(1)
  7. 功能:回到历史记录的前一步
  8. 相当于history.forward()
  9. 语法:history.go(-n)
  10. 功能:回到历史记录的前n
  11. 语法:history.go(n)
  12. 功能:回到历史记录的后n

3-4.screen对象

  1. screen对象包含有关客户端显示屏幕的信息
  2. screen.availWidth
  3. screen.availHeight

例子

  1. <script>
  2. //获取屏幕的宽度
  3. var screenWidth=window.screen.availWidth;
  4. console.log(screenWidth);
  5. //获取屏幕可视区
  6. var viewWidth=document.body.clientWidth;
  7. console.log(viewWidth);
  8. </script>

3-5.navigator对象

  1. 1.掌握Navigator对象的userAgent属性
  2. 2.掌握如何判断浏览器的类型
  3. 3.掌握如何判断设备的终端是移动还是PC
  4. UserAgent:用来识别浏览器名称,版本,引擎以及操作系统等信息的内容。
  5. //检测浏览器类型
  6. if(/Android|iphone|webOS/i.test(navigator.userAgent)){
  7. location.href="mobile.html"
  8. }else if(/ipad/i.test(navigator.userAgent)){
  9. location.href="pad.html"
  10. }

例子

  1. //01.html
  2. <p>01</p>
  3. <a href="02.html">02.html</a>
  1. //02.html
  2. <p>02</p>
  3. <button id="btn">click me</button>
  4. <script>
  5. var btn=document.getElementById("btn");
  6. btn.onclick=function(){
  7. history.back();
  8. }
  9. </script>