JavaScript——BOM学习

BOM基础

BOM(浏览器对象模型),BOM提供了很多对象,用于访问浏览器的功能。

window对象(当前的浏览器窗口)

window对象是BOM的核心,表示浏览器的一个实例。在浏览器中,window对象有双重角色,既是通过JavaScript访问浏览器窗口的一个接口,又是ECMAScript规定的Global(全局对象),任何一个对象、变量和函数,都以window作为其Global对象。

window对象方法有如下:
这里写图片描述

全局函数声明

两种方法:

  1. window声明:window.函数名=function( ) { }
  2. 关键字声明:function 函数名( ) { }

window.open()方法

语法:window.open( url , 窗口名称 , 参数字符 )

  1. url:要打开的网站地址

  2. 窗口名称

    • _blank:在新窗口显示目标网页
    • _self:在当前窗口显示目标网页
    • _top:框架网页中在上部窗口中显示目标网页
  3. 参数字符
    这里写图片描述
  4. 例子
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>window.open</title>
<script type="text/javascript">
  function Wopen(){
      window.open('http://www.imooc.com','_blank','width=300,height=200,menubar=no,toolbar=no, status=no,scrollbars=yes')

  } 
</script>
</head>
<body>
    <input name="button" type="button" onClick="Wopen()" value="点击我,打开新窗口!" / >
</body>
</html>

window.close( )方法

语法:window.close( ) 或者 “对象”.close( )

<script type="text/javascript">
   var mywin=window.open('http://www.imooc.com'); //将新打的窗口对象,存储在变量mywin中
   mywin.close();
</script>

window.alert( ) 和window.confirm( )方法

这里写图片描述

  <body>
  <button id="but">点击</button>
  <script type="text/javascript">
      var but=document.getElementById("but");
      but.onclick=function () {
          var result=window.confirm("确定要删除?");
          if(result){
              document.getElementById("but").style.display="none";
          }
      }
  </script>
  </body>

window.prompt( )方法

这里写图片描述

  <script type="text/javascript">
      var message=prompt("请输入您的星座","天蝎座");
      console.log(message);
  </script>

window.setTimeout( )和window.clearTimeout( )

这里写图片描述

这里写图片描述

    <script>
       var fnCall=function(){
          alert("world");
       }
       var timeout1=setTimeout(function(){
          alert("hello");
       },2000)  //延迟两秒调用
       setTimeout(fnCall,5000);//延迟5秒调用
    </script>



    <script>
       var fnCall=function(){
          alert("world");
       }
       var timeout1=setTimeout(function(){
          alert("hello");
       },2000)  //延迟两秒调用
       clearTimeout(timeout1);
    </script>

setInterval()和clearInterval()

这里写图片描述

这里写图片描述
1、计时器例子:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定时器</title>
<script type="text/javascript">
  var attime;
  function clock(){
    var time=new Date();          
    attime= time.getHours()+":"+time.getMinutes()+":"+time.getSeconds() ;
    document.getElementById("clock").value = attime;
  }
  var int=setInterval(clock, 1)//设置指定时间内运行
</script>
</head>
<body>
<form>
<input type="text" id="clock" size="50"  />
</form>
</body>
</html>

2、时钟停止和开始的控制

  <body>
  <script type="text/javascript">
      function clock(){
          var time=new Date();
          document.getElementById("clock").value = time;
      }
      // 每隔100毫秒调用clock函数,并将返回值赋值给i
      var i=setInterval(clock,100);
      //控制时间开始
      function beginTime() {
          var i=setInterval(clock,100);
      }


  </script>
  </head>
  <body>
  <form>
    <input type="text" id="clock" size="50"  />
    <input type="button" value="Stop" onclick="clearInterval(i)"  />
    <input type="button" value="Begin" onclick="beginTime() ">
  </form>
  </body>

3、控制打印数字

   // 每隔1秒针num递增一次,直到num的值等于max清除
         var num=1,
            max=10,
            timer=null;
<script>
       function inCreamentNum(){
           console.log(num);   // 1 2 3 10 
           num++;      
           if(num<=max){
              setTimeout(inCreamentNum,1000);
           }else{
              clearTimeout(timer);
           }
       }
       timer=setTimeout(inCreamentNum,1000);
    </script>

Location对象

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

功能:用于获取或设置窗体的URL,并可以用于解析URL
语法:location.属性或方法

Location对象属性

这里写图片描述

Location对象属性分布

这里写图片描述

Location对象方法

这里写图片描述
这里写图片描述

例子

1、使用location.hash返回顶部
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
       .box1{height:400px;background:#ccc;}
       .box2{height:600px;background:#666;}
    </style>
</head>
<body>
    <div class="box1" id="top"></div>
    <div class="box2"></div>
    <input type="button" id="btn" value="返回顶部">
    <script>
       //console.log(location.href);
       //console.log(location.hash);//获取url地址后面是否有"#XXX"如www.sohu.com#XXX
       var btn=document.getElementById("btn");
       btn.onclick=function(){
          location.hash="#top";
       }
       console.log(location.pathname);
    </script>
</body>
</html>
2、普通用法
//假设初始 URL 为 http://www.wrox.com/WileyCDA/
//URL 修改为"http://www.wrox.com/WileyCDA/#section1"
location.hash ------->"#section1";
//URL 修改为"http://www.wrox.com/WileyCDA/?q=javascript"
location.search ------->"?q=javascript";
//URL 修改为"http://www.yahoo.com/WileyCDA/"
location.hostname -------> "www.yahoo.com";
//URL 修改为"http://www.yahoo.com/mydir/"
location.pathname -------> "mydir";
//URL 修改为"http://www.yahoo.com:8080/WileyCDA/"
location.port -------> 8080;
3、location的两个方法
<body>
    <input type="button" value="刷新" id="reload">
    <script>
      /*  setTimeout(function(){
            //location.href='index6.html';
            // window.location='index6.html';
            location.replace("index6.html");
        },1000)*/
         document.getElementById("reload").onclick=function(){
             location.reload(true);
         }
    </script>
</body>

history对象

history 对象保存着用户上网的历史记录,从窗口被打开的那一刻算起。因为 history 是 window对象的属性,因此每个浏览器窗口、每个标签页乃至每个框架,都有自己的 history 对象与特定的window 对象关联。出于安全方面的考虑,开发人员无法得知用户浏览过的 URL。

语法:window.histroy.方法或属性

history对象属性

属性描述
length返回浏览器历史列表中的url数量
使用length属性,当前窗口的浏览历史总长度,代码如下:

<script type="text/javascript">
  var HL = window.history.length;
  document.write(HL);
</script>

history对象方法

属性描述
back()加载history列表中前一个url
forward()加载history列表下一个url
go()加载history列表中某个具体的url
返回前一个浏览的页面
  • back()方法,加载 history 列表中的前一个 URL。
语法:window.history.back();

比如,返回前一个浏览的页面,代码如下:
window.history.back();
注意:等同于点击浏览器的倒退按钮。
back()相当于go(-1),代码如下:
window.history.go(-1);
返回下一个浏览的页面
  • forward()方法,加载 history 列表中的下一个 URL。
如果倒退之后,再想回到倒退之前浏览的页面,则可以使用forward()方法,代码如下:
window.history.forward();
注意:等价点击前进按钮。
forward()相当于go(1),代码如下:
window.history.go(1);

Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本。

这里写图片描述

查看浏览器的名称和版本,代码如下:


<script type="text/javascript">
   var browser=navigator.appName;
   var b_version=navigator.appVersion;
   document.write("Browser name"+browser);
   document.write("<br>");
   document.write("Browser version"+b_version);
</script>
使用userAgent判断使用的是什么浏览器
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>navigator</title>
<script type="text/javascript">
  function validB(){ 
    var u_agent = navigator.userAgent; 
    var B_name="不是想用的主流浏览器!"; 
    if(u_agent.indexOf("Firefox")>-1){ 
        B_name="Firefox"; 
    }else if(u_agent.indexOf("Chrome")>-1){ 
        B_name="Chrome"; 
    }else if(u_agent.indexOf("MSIE")>-1&&u_agent.indexOf("Trident")>-1){ 
        B_name="IE(8-10)";  
    }
        document.write("浏览器:"+B_name+"<br>");
        document.write("u_agent:"+u_agent+"<br>"); 
  } 
</script>
</head>
<body>
  <form>
     <input type="button" value="查看浏览器"   onclick=validB()>
  </form>
</body>
</html>

screen对象

用于获取用户的屏幕信息
语法:window.screen.属性

对象属性

这里写图片描述

屏幕分辨率的高和宽

window.screen 对象包含有关用户屏幕的信息。
1. screen.height 返回屏幕分辨率的高
2. screen.width 返回屏幕分辨率的宽

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>屏幕分辨率的高和宽</title>
</head>
<body>
<script type="text/javascript">
  document.write( "屏幕宽度:"+screen.width+"px<br />" );
  document.write( "屏幕高度:"+screen.height+"px<br />" );
</script>
</body>
</html>
屏幕可用高和宽度
  1. screen.availWidth 属性返回访问者屏幕的宽度,以像素计,减去界面特性,比如任务栏。
  2. screen.availHeight 属性返回访问者屏幕的高度,以像素计,减去界面特性,比如任务栏。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>屏幕分辨率的高和宽</title>
</head>
<body>
<script type="text/javascript">
document.write("可用宽度:" + screen.availWidth);
document.write("可用高度:" + screen.availHeight);
</script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值