window
The Window Object
window.document.getElementById("header");
document.getElementById("header");
Window Size
- window.innerHeight - the inner height of the browser window (in pixels)
- window.innerWidth - the inner width of the browser window (in pixels)
The browser window (the browser viewport) is NOT including toolbars and scrollbars.
let w = window.innerWidth; let h = window.innerHeight;
Other Window Methods
window.open() - open a new window
- window.close() - close the current window
- window.moveTo() - move the current window
- window.resizeTo() - resize the current window
Screen
Window Screen
The window.screen object can be written without the window prefix.
Properties:
- screen.width
- screen.height
- screen.availWidth
- screen.availHeight
- screen.colorDepth
-
Window Screen Width
The screen.width property returns the width of the visitor’s screen in pixels.
Example
Display the width of the screen in pixels:
document.getElementById("demo").innerHTML = "Screen Width: " + screen.width;
Window Screen Height
document.getElementById("demo").innerHTML = "Screen Height: " + screen.height;
Window Screen Available Width
screen.availWidth 属性返回访问者屏幕的宽度,以像素为单位,减去 Windows 任务栏等界面功能。
document.getElementById("demo").innerHTML = "Available Screen Width: " + screen.availWidth;
Window Screen Available Height
Window Screen Color Depth
document.getElementById("demo").innerHTML = "Screen Color Depth: " + screen.colorDepth;
screen.colorDepth 属性返回用于显示一种颜色的位数。 所有现代计算机都使用 24 位或 32 位硬件进行颜色分辨率: 24 位 = 16,777,216 种不同的“真彩色” 32 位 = 4,294,967,296 种不同的“深色” 较旧的计算机使用 16 位:65,536 种不同的“高色彩”分辨率。 非常旧的计算机和旧手机使用 8 位:256 种不同的“VGA 颜色”。
Window Screen Pixel Depth
screen.pixelDepth 属性返回屏幕的像素深度。
document.getElementById("demo").innerHTML = "Screen Pixel Depth: " + screen.pixelDepth;
Location
window.location 对象可用于获取当前页面地址 (URL) 并将浏览器重定向到新页面。
The window.location object can be written without the window prefix.
Some examples: window.location.href returns the href (URL) of the current page
- window.location.hostname returns the domain name of the web host
- window.location.pathname returns the path and filename of the current page
- window.location.protocol returns the web protocol used (http: or https:)
- window.location.port 端口
- window.location.assign() loads a new document
Window Location Assign
```javascript
直接将新的网址覆盖本html
<a name="YzBhL"></a>
# history
- history.back() - same as clicking back in the browser
- history.forward() - same as clicking forward in the browser
- /d
```html
<html>
<head>
<script>
function goBack() {
window.history.back()
}
</script>
</head>
<body>
<input type="button" value="Back" onclick="goBack()">
</body>
</html>
Navigator
window.navigator 对象包含有关访问者浏览器的信息。
The window.navigator object can be written without the window prefix.
Some examples:
- navigator.appName
- navigator.appCodeName
- navigator.platform
Browser Cookies
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"cookiesEnabled is " + navigator.cookieEnabled;
</script>
navigator感觉好多没用,用到再说把