如果你想成为一名优秀的java架构师,看这里 → 乐字节免费公开课(腾讯课堂)

如需要跟多资料请点击右侧(记住入群暗号:66) → 这是一条不归路,有秃头风险,请慎重选择!

BOM对象

BOM的核心对象是window,它表示浏览器的一个实例。window对象有双重角色,它既是通过JavaScript访问浏览器窗口的一个接口,又是ECMAScript规定的Global对象。这意味着在网页中定义的任何一个对象、变量和函数,都以window作为其Global对象,因此有权访问parseInt()等方法。如果页面中包含框架,则每个框架都拥有自己的window对象,并且保存在frames集合中。在frames集合中,可以通过数值索引(从0开始,从左至右,从上到下)或者框架的名称来访问相应的window对象。

Window对象方法

系统对话框

浏览器通过(实际是window对象的方法)alert()、confirm()、prompt()方法可以调用系统对话框向用户显示消息。

(1)消息框:alert, 常用。    alert() 方法用于显示带有一条指定消息和一个 OK 按钮的警告框。(2)输入框:prompt,返回提示框中的值。    prompt() 方法用于显示可提示用户进行输入的对话框。    参数(可选):       第一个参数:要在对话框中显示的纯文本。        第二个参数:默认的输入文本。(3)确认框:confirm,返回 true/false.confirm() 方法用于显示一个带有指定消息和 OK 及取消按钮的对话框。
<style type="text/css">    #aa{        border: 1px solid red;        height: 100px;    }</style><body>    <div id="aa">        This is a div    </div>    <button onclick="testAlert();">警告</button>    <button onclick="testComfirm();">修改</button>    <button onclick="testPrompt();">输入</button>    <script type="text/javascript">        // 1.警告框        function testAlert(){            alert('警告框!!!');        }                        /*         2.输入框             返回值:输入的内容         * */        function testPrompt(){            var item = prompt('请输入年龄'); // item得到输入的值            // console.log(item)            // alert(prompt('请输入年龄',18)); // 将输入的值输出        }                /*         3.确认框             返回值:boolean(true|false)         * */        function testComfirm(){            var result = confirm('真的要改吗?');            if(result){                var ele = document.getElementById("aa");                ele.style.color="red";                ele.innerHTML="<span>fdsfsd</span>";            }else{                alert("没事别瞎点。。。");            }        }    </script></body>

打开窗口

window.open()方法既可以导航到一个特定的URL也可以用来打开一个新的窗口

<script type="text/javascript">function openBaidu(){    window.open('http://www.baidu.com','_self'); // _self、_blank等    // window.open();        //空白窗口}</script><input type="button" name="open" value="百度" onclick='openBaidu();' />

关闭窗口

window.close():关闭窗口。

例:点击按钮关闭当前窗口。

<input type="button" value="关闭窗口" onclick="window.close();" />

时间函数

setTimeout()

setTimeout() : 在指定的毫秒数后调用函数或计算表达式。返回一个唯一的标识;也可以通过返回的标识cliearTimeout(id): 来清除指定函数的执行。

var id = setTimeout(function,times);
clearTimeout(id);

示例:

<script type="text/javascript">    // 延迟3 秒后出现 alert    function hello() {           alert("对不起, 要你久候");     }    setTimeout("hello()", 3000);        // 时间显示器    var timeout;    function init(){           // 拿到当前时间           var date = new Date();           var time = date.toLocaleString();           // 拿到相应对象             var h1 = document.getElementById('h1');           // 根据需求添加样式           if(0==date.getSeconds()){    // 当时间的秒数变成0时,显示红色字体               h1.innerHTML = '<span style="color:red">' + time + '</span>';           } else {               h1.innerHTML = time;           }           /*            *     定时操作,只执行一次                 第一个参数:执行的方法;第二个参数:定时,单位是毫秒            * */            setTimeout(init,1000);   // 等多少时间来执行    }    // window.setTimeout(init,1000);// 只执行一次            // 停止操作    function stopShow () {        clearTimeout(timeout);    }</script><body onload="init();">    <h1 id="h1"></h1>    <button onclick="stopShow()">时间停止</button></body>

在times毫秒后执行function指定的方法,执行之前也可以取消

setInteval()

setInterval():可按照指定的周期(以毫秒计)来调用函数或计算表达式,也可根据返回的标识用来结束。该方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。

var id = setInterval(function,times);
clearInterval(id);
function test(){    console.log(".....");}// window是一个全局对象,通过全局对象调用setInterval()函数window.setInterval(test,1000);

history对象

history 对象是历史对象。包含用户(在浏览器窗口中)访问过的 URL。history 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问。

history对象的属性:length,返回浏览器历史列表中的 URL 数量。

history对象的方法:

back():加载 history 列表中的前一个 URL。

forward():加载历史列表中的下一个 URL。当页面第一次访问时,还没有下一个url。

go(number|URL): URL 参数使用的是要访问的 URL。而 number 参数使用的是要访问的 URL 在 History 的 URL 列表中的相对位置。go(-1),到上一个页面

013-history.html

<body>    <a href="013-history-a.html">013-history-a.html</a>    <h1>我是第一个页面</h1>    <input type="button"  value="前进" onclick="window.history.forward();" />    <script>        console.log(window.history);    </script></body>

013-history-a.html

<body>    <a href="013-history-b.html">013-history-b.html</a>    <h1>我是A页面</h1>    <input type="button" value="后退"  onclick="window.history.back();"/></body>

013-history-b.html

<body>      <h1>我是B页面</h1>      <input type="button" value="第一个页面" onclick="window.history.go(-2);"/>      <input type="button" value="后退"  onclick="window.history.back();"/></body>

location对象

location 对象是window对象之一,提供了与当前窗口中加载的文档有关的信息,还提供了一些导航功能。也可通过 window.location 属性来访问。

location 对象的属性 href:设置或返回完整的 URL

location 对象的方法

reload():重新加载当前文档。

replace():用新的文档替换当前文档。

<script type="text/javascript">    function openBaidu(){        // 没有历史记录,用新的文档替换当前文档        // window.location.replace("http://www.baidu.com");        // console.log(window.location.href); // 获取完整的url        window.location.href = "http://www.baidu.com";    }</script><body>    <input type="text"  value="" />    <input type="button" value="刷新" onclick="window.location.reload();" />    <input type="button"  value="百度" onclick="openBaidu();" /></body>