[JavaScript基礎(chǔ)]學(xué)習(xí)②⑧--瀏覽器對(duì)象

window

innerWidth和innerHeight屬性 獲取瀏覽器窗口的內(nèi)部寬度和高度

兼容性:IE<=8不支持。

'use strict';
// 可以調(diào)整瀏覽器窗口大小試試:
alert('window inner size: ' + window.innerWidth + ' x ' + window.innerHeight);

outerWidth和outerHeight屬性,可以獲取瀏覽器窗口的整個(gè)寬高

navigator

navigator.appName 瀏覽器名稱(chēng);

navigator.appVersion 瀏覽器版本;

navigator.language 瀏覽器設(shè)置的語(yǔ)言;

navigator.platform 操作系統(tǒng)類(lèi)型;

navigator.userAgent 瀏覽器設(shè)定的User-Agent字符串。

'use strict';
alert('appName = ' + navigator.appName + '\n' +
      'appVersion = ' + navigator.appVersion + '\n' +
      'language = ' + navigator.language + '\n' +
      'platform = ' + navigator.platform + '\n' +
      'userAgent = ' + navigator.userAgent);

navigator的信息可以很容易地被用戶(hù)修改,所以JavaScript讀取的值不一定是正確的

var width;
if (getIEVersion(navigator.userAgent) < 9) {
    width = document.body.clientWidth;
} else {
    width = window.innerWidth;
}

改用短路運(yùn)算符||

var width = window.innerWidth || document.body.clientWidth;

screen

screen.width 屏幕寬度,以像素為單位;

screen.height 屏幕高度,以像素為單位;

screen.colorDepth 返回顏色位數(shù),如8、16、24

'use strict';
alert('Screen size = ' + screen.width + ' x ' + screen.height);

location

一個(gè)完整的URL

http://www.example.com:8080/path/index.html?a=1&b=2#TOP
location.href;//http://www.example.com:8080/path/index.html?a=1&b=2#TOP
location.protocol; // 'http'
location.host; // 'www.example.com'
location.port; // '8080'
location.pathname; // '/path/index.html'
location.search; // '?a=1&b=2'
location.hash; // 'TOP'

加載一個(gè)新頁(yè)面

location.assign()

重新加載當(dāng)前頁(yè)面

location.reload()
'use strict';
if (confirm('重新加載當(dāng)前頁(yè)' + location.href + '?')) {
    location.reload();
} else {
    location.assign('/discuss'); // 設(shè)置一個(gè)新的URL地址
}

document

document對(duì)象表示當(dāng)前頁(yè)面。由于HTML在瀏覽器中以DOM形式表示為樹(shù)形結(jié)構(gòu),document對(duì)象就是整個(gè)DOM樹(shù)的根節(jié)點(diǎn)。

'use strict';
document.title = '努力學(xué)習(xí)JavaScript!';//<title>xxx</title>

getElementById() 按ID獲得一個(gè)DOM節(jié)點(diǎn)

getElementsByTagName() 按Tag名稱(chēng)獲得一組DOM節(jié)點(diǎn)

<dl id="drink-menu" style="border:solid 1px #ccc;padding:6px;">
    <dt>摩卡</dt>
    <dd>熱摩卡咖啡</dd>
    <dt>酸奶</dt>
    <dd>北京老酸奶</dd>
    <dt>果汁</dt>
    <dd>鮮榨蘋(píng)果汁</dd>
</dl>
'use strict';
var i, s, menu, drinks;

menu = document.getElementById('drink-menu');
menu.tagName; // 'DL'

drinks = document.getElementsByTagName('dt');
s = '提供的飲料有:';
for (i=0; i<drinks.length; i++) {
    s = s + drinks[i].innerHTML + ',';
}
alert(s);

cookie

Paste_Image.png

讀取到當(dāng)前頁(yè)面的Cookie

document.cookie; // 'v=123; remember=true; prefer=zh'
Paste_Image.png

history

history對(duì)象保存了瀏覽器的歷史記錄
不推薦使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • JavaScript 快速入門(mén) 瀏覽器瀏覽器對(duì)象操作 DOM插入 DOM刪除 DOM 操作表單獲取值設(shè)置值HTML...
    染微言閱讀 1,793評(píng)論 0 5
  • 1. 本文是在學(xué)習(xí)廖雪峰先生的JavaScrip教程 后的歸納 一、瀏覽器對(duì)象 常用的瀏覽器: Chrome,Fi...
    ting723閱讀 507評(píng)論 0 3
  • 一、看包裝(3min) 1.主標(biāo)題:《非暴力溝通》 副標(biāo)題: 2.建議上架類(lèi)別:心靈 勵(lì)志 3.作者:【美】馬歇...
    Jean_zz閱讀 373評(píng)論 0 0
  • 終于離家了,回到了廣州。 在家的時(shí)候,太多回憶和往事?lián)涿娑鴣?lái),走過(guò)的街道,仰望過(guò)的星空,聽(tīng)過(guò)的歌,總會(huì)讓我繞過(guò)好幾...
    醒時(shí)只愿朝花笑閱讀 331評(píng)論 0 0

友情鏈接更多精彩內(nèi)容