若是你想成爲一名優秀的java架構師,看這裏 → 樂字節免費公開課(騰訊課堂)javascript
如須要跟多資料請點擊右側(記住入羣暗號:66) → 這是一條不歸路,有禿頭風險,請慎重選擇!css
BOM的核心對象是window,它表示瀏覽器的一個實例。window對象有雙重角色,它既是經過JavaScript訪問瀏覽器窗口的一個接口,又是ECMAScript規定的Global對象。這意味着在網頁中定義的任何一個對象、變量和函數,都以window做爲其Global對象,所以有權訪問parseInt()等方法。若是頁面中包含框架,則每一個框架都擁有本身的window對象,而且保存在frames集合中。在frames集合中,能夠經過數值索引(從0開始,從左至右,從上到下)或者框架的名稱來訪問相應的window對象。html
瀏覽器經過(實際是window對象的方法)alert()、confirm()、prompt()方法能夠調用系統對話框向用戶顯示消息。java
(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() : 在指定的毫秒數後調用函數或計算表達式。返回一個惟一的標識;也能夠經過返回的標識cliearTimeout(id): 來清除指定函數的執行。ide
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指定的方法,執行以前也能夠取消字體
setInterval():可按照指定的週期(以毫秒計)來調用函數或計算表達式,也可根據返回的標識用來結束。該方法會不停地調用函數,直到 clearInterval() 被調用或窗口被關閉。
var id = setInterval(function,times);
clearInterval(id);
function test(){ console.log("....."); } // window是一個全局對象,經過全局對象調用setInterval()函數 window.setInterval(test,1000);
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 對象是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>