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ì)象保存了瀏覽器的歷史記錄
不推薦使用