亲宝软件园·资讯

展开

JavaScript 浏览器对象模型BOM原理与常见用法实例分析

人气:0

本文实例讲述了JavaScript 浏览器对象模型BOM原理与常见用法。分享给大家供大家参考,具体如下:

什么是BOM

BOM:Browser Object Model,浏览器对象模型

从上图也可以看出:

window对象:

弹出系统对话框

比如说,alert(1)是window.alert(1)的简写,因为它是window的子方法。

系统对话框有三种:

alert(); //不同浏览器中的外观是不一样的
confirm(); //兼容不好
prompt(); //不推荐使用

打开窗口、关闭窗口

打开窗口

window.open(url,target)

参数解释:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
 </head>
 <body>
  <!--行间的js中的open() window不能省略-->
  <button onclick="window.open('https://www.luffycity.com/')">路飞学城</button>
  <button>打开百度</button>
  <button onclick="window.close()">关闭</button>
  <button>关闭</button>
 </body>
 <script type="text/javascript">
  var oBtn = document.getElementsByTagName('button')[1];
  var closeBtn = document.getElementsByTagName('button')[3];
  oBtn.onclick = function(){
      //open('https://www.baidu.com')
   //打开空白页面
   open('about:blank',"_self")
  }
  closeBtn.onclick = function(){
   if(confirm("是否关闭?")){
    close();
   }
  }
 </script>
</html>
<body>
<!--BOM使用的是window.开头的语句-->
<!--history模式和 hash模式-->
<!--history模式: xxxx/#/index.html-->
<!--hash模式:xxxx/index.html-->
<button id="btn">跳转</button>
<script>
 var oBtn=document.getElementById('btn');
 oBtn.onclick=function(){
  console.log(location);
  //打开百度,下面三种方式皆可
  // location.href='http://www.baidu.com';
  // open('http://www.baidu.com','_self');
  window.open('http://www.baidu.com','_self');//在当前页面打开
  window.location.reload();//刷新或者叫重载
 }
</script>
</body>

location对象

window.location可以简写成location。location相当于浏览器地址栏,可以将url解析成独立的片段。

location对象的属性

举例:5秒后自动跳转到百度。

有时候,当我们访问一个不存在的网页时,会提示5秒后自动跳转到指定页面,此时就可以用到location

<script>
 setTimeout(function () {
  location.href = "http://www.baidu.com";
 }, 5000);
</script>

location.reload():重新加载,这个加载是  全局刷新,让整个文档重新解析了一遍,一般不建议使用

setTimeout(function(){
   //3秒之后让网页整个刷新
 window.location.reload();  
},3000)

navigator对象

window.navigator 的一些属性可以获取客户端的一些信息。

console.log(navigator.userAgent);
console.log(navigator.platform);

希望本文所述对大家JavaScript程序设计有所帮助。

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论