讀書時間《JavaScript進階程式設計》四:BOM,用戶端檢測

來源:互聯網
上載者:User

標籤:

  隔了一段時間,現在開始看第8章。

第8章:BOM

  BOM提供了很多個物件,用於訪問瀏覽器的功能。BOM的核心對象是window,它表示瀏覽器的一個執行個體。

  window對象是通過javascript訪問瀏覽器視窗的一個介面,又是ECMAScript中的Global對象。

  所有在全域範圍聲明的變數、函數都是window對象的屬性和方法。

  1. 視窗關係,架構

    每個架構都有window對象,並在frames集合中

    每個window對象都有name屬性,其中包含架構名稱

    top對象始終指向最高(最外)層架構,也就是瀏覽器視窗

    parent對象始終指向當前架構的直接上層架構

    self對象始終指向window對象

  2. 視窗位置、視口大小

var leftPos = (typeof window.screenLeft == "number") ?   window.screenLeft : window.screenX;var topPos = (typeof window.screenTop == "number") ?  window.screenTop : window.screenY;var pageWidth = document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;var pageHeight = document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;

   3. 開啟新視窗  

function openNewWindow(){    var win = window.open(‘http://www.a.com/‘, ‘aWindow‘, ‘width=300,height=300,left=10,top=10‘);    win.resizeTo(500,500);    win.moveTo(100,100);    //win.close();    //win.opener = null;} 

   4. 間隔調用 逾時調用

//1秒後執行 只執行一次var timeoutId = setTimeout(function() {    alert("Hello world!");}, 1000);//clearTimeout(timeoutId);//每間隔1秒執行一次,直到取消var timer = setInterval(function() {     alert("Hello world!"); }, 1000); //clearInterval(timer);//相容 requestAnimationFrame 用法和setTimeout類似if (!window.requestAnimationFrame) {  window.requestAnimationFrame = (window.webkitRequestAnimationFrame ||                                  window.mozRequestAnimationFrame ||                                  window.msRequestAnimationFrame ||                                  window.oRequestAnimationFrame ||                                  function (callback) {                                    return window.setTimeout(callback, 17);                                  });}if (!window.cancelRequestAnimationFrame) {  window.cancelRequestAnimationFrame = (window.cancelAnimationFrame ||                                        window.webkitCancelRequestAnimationFrame ||                                        window.mozCancelRequestAnimationFrame ||                                        window.msCancelRequestAnimationFrame ||                                        window.oCancelRequestAnimationFrame ||                                        window.clearTimeout);}

   5. 系統對話方塊

//alertalert(‘Hello world!‘);//confirmif(confirm(‘are you ok?‘)){//yes}else{//no}//promptvar result = prompt("What is your name? ", "");if(result !== null) { alert("Welcome, " + result);}

   6. location對象

// http://www.a.com:1234/test.html?k1=v1&k2=v2#part2location.hash//URL的錨部分 ( #part2 ) location.hash=‘#hash‘ //不會重新載入location.host//URL的主機名稱和連接埠號碼location.href//完整URLlocation.pathname//URL的路徑部分 ( /test.html )location.port//URL的連接埠部分 ( 1234 )location.protocol//URL的協議 ( http )location.search//URL的查詢部分 ( ?k1=v1&k2=v2 )location.assign//載入一個新的文檔window.location.assign("http://www.a.com")window.location=‘http://www.a.com/‘;location.href=‘http://www.a.com/‘;location.reload //重新載入當前文檔location.reload() //重新載入 可能重緩衝載入location.reload(true) //重伺服器重新載入location.replace //用一個新文檔取代當前文檔location.replace(‘www.a.com‘) //不會留下記錄 不能點擊後退返回上一個頁面//轉到新URLlocation.assign(‘www.a.com‘)window.location=‘www.a.com‘;location.href=‘www.a.com‘;

   7. navigator對象 

navigator.appName //名稱navigator.appVersion //版本navigator.appCodeName //代碼名稱navigator.platform //系統平台navigator.cookieEnabled //是否啟用cookies navigator.userAgent //瀏覽器使用者代理程式頭的值(唯讀字串)if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){location.href="http://www.a.com/";}else if(/iPad/i.test(navigator.userAgent)){  location.href="http://www.b.com/"}else{location.href="http://www.c.com/"}

   8. history對象

back() //載入曆史列表中的前一個URL(如果存在)等價於點擊後退按鈕或調用 history.go(-1)forward() //載入曆史列表中的下一個URL 等價於點擊前進按鈕或調用 history.go(1)go() //載入曆史列表中的某個具體的頁面//後退一頁history.back()  history.go(-1) //前進一頁history.forward()  history.go(1) 

 第9章:用戶端檢測

   檢測web用戶端的方式很多,不到萬不得已不要使用用戶端檢測,有更通用的方法就使用更通用的方法。

   最常用的用戶端檢測形式是能力檢測(特性檢測),判斷瀏覽器是否支援某種特性。

//基本檢測格式if(object.property){//use object.property}//ie判斷var isIE = !!("ActiveXObject" in window);var isIE6 = !document.addEventListener && !window.XMLHttpRequest;var isIE7 = isIE && !!window.XMLHttpRequest && !document.documentMode;var isIE8 = isIE && !document.addEventListener && !!document.documentMode;if( isIE6 ) alert(‘你使用的是ie6瀏覽器‘)if( isIE7 ) alert(‘你使用的是ie7瀏覽器‘)if( isIE8 ) alert(‘你使用的是ie8瀏覽器‘)

   在可能的情況下,儘可能使用 typeof 來檢測。

   使用者代理程式檢測包含大量與瀏覽器有關的資訊。 

var ua = navigator.userAgent; if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(ua)){location.href="http://www.a.com/";}else if(/iPad/i.test(ua)){  location.href="http://www.b.com/"}else{location.href="http://www.c.com/"}

   第8章主要介紹了瀏覽器的window對象,window對象下的各種方法、navigator對象、history對象。第9章介紹怎樣檢查用戶端,在平常為了相容一般都要做這樣的檢測。根據瀏覽器是否支援某個特性來檢測,根據瀏覽器的navigator.userAgent(使用者代理程式資訊)檢測。

 

  

  

讀書時間《JavaScript進階程式設計》四:BOM,用戶端檢測

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.