JavaScript BOM核心对象详解:window、location、history与document

作者:像素猫时间:2026-10-30 17:36:54浏览:434

本文深入解析JavaScript浏览器对象模型BOM,重点介绍顶层对象window及其核心子对象。详细阐述location对象的URL读写与页面跳转、history对象的历史记录机制、navigator与screen的浏览器及屏幕信息获取,以及document对页面元素集合的访问与操作。

BOM概述与核心对象

本文通过具体实例详细解析JavaScript中的浏览器对象模型(BOM),旨在为读者提供实用的参考与学习素材。

在BOM的层级结构中,位于最顶层,它是所有其他子对象的容器。这些子对象包括location、navigator、document、screen和history。其中,负责存储当前页面的URL信息。该对象的部分属性为只读,而href属性则支持读写操作。开发者既可以通过读取href属性获取当前URL,也可以通过修改href属性实现页面的跳转导航。

window与location对象详解


<html>
  <body>
    <script type='text/javaScript'>
      window.location.replace("http://www.baidu.com");
      window.location.href="http://www.sina.com";
    </script>
  </body>
</html>

用于保存用户自打开浏览器以来访问过的页面历史记录。需要注意的是,并非所有页面都会被记录,例如通过location对象的replace()方法加载的页面将不会出现在历史记录中。代表了浏览器本身的特性,提供了版本号、浏览器类型以及用户操作系统等关键信息。则包含了客户端显示能力的相关信息,如屏幕分辨率和色彩深度等。

history、navigator与screen对象


<html>
  <body>
    <script type='text/javascript'>
      switch(window.screen.colorDepth){
        case 1:
        case 4:
          document.bgColor = "white";
          break;
        case 8:
        case 15:
        case 16:
          document.bgColor = "blue";
          break;
        case 24:
        case 32:
          document.bgColor = "skyblue";
          break;
        default:
          document.bgColor = "white";
      }
      document.write("Your screen supports "+window.screen.colorDepth + " bit color");
    </script>
  </body>
</html>

document是最关键的對象之一。document對象包含了三個數組屬性links[],images[],forms[]。這三個數組分別代表了頁面中所有由<A>、<img>、<form>所創建對象的集合。

document对象与页面元素操作


<html>
  <body>
    <img name=img1 src="images/1.jpg" border=0 width=200 height=150>
    <script type='text/javaScript'>
      var myImages = new Array("images/1.jpg","images/2.jpg","images/3.jpg","images/4.jpg");
      var imgIndex = prompt("Enter a number from 0 to 3","");
      document.images['img1'].src = myImages[imgIndex];
    </script>
  </body>
</html>


<html>
  <head>
    <script type='text/javascript'>
      var imagesArray=new Array("images/1.jpg","images/2.jpg","images/3.jpg","images/4.jpg");
      function changeImg(imageNumber){
        var newImage = imagesArray[Math.round(Math.random()*3)];
        alert(document.images[imageNumber].src);
        while(document.images[imageNumber].src.indexOf(newImage)!=-1){
          newImage = imagesArray[Math.round(Math.random()*3)];
        }
        document.images[imageNumber].src = newImage;
        return false;
      }
    </script>
  </head>
  <body>
    <img name='img1' src="images/1.jpg" width=150 height=200 onclick="return changeImg(0)">
    <img name='img2' src="images/2.jpg" width=150 height=200 onclick="return changeImg(1)">
  </body>
</html>


<html>
  <head>
    <script type='text/javascript'>
      function linkPage(){
        alert('You Clicked?');
        return false;
      }
    </script>
  </head>
  <body>
    <A href='http://www.baidu.com' name='link' onclick="return linkPage()">
      Click Me
    </A>
  </body>
  <script type='text/javaScript'>
    window.document.links['link'].href="http://www.google.com";
  </script>
<html>

利用上述三个数组,开发者可以便捷地访问页面中的特定元素。例如,通过修改images数组中img对象的属性,可以动态更改页面上的图片;同样,通过修改links数组中超链接对象的属性,可以改变超链接指向的URL地址。

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript操作DOM技巧总结》、《JavaScript替换操作技巧总结》、《JavaScript传值操作技巧总结》、《javascript编码操作技巧总结》、《JavaScript中json操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》

希望本文所述内容能对大家的JavaScript程序设计工作有所帮助。

作者最新文章