本文档描述了现代Web浏览器提供的标准环境规格,包括核心API、对象模型和交互能力。
window.innerWidth - 视口宽度(px)window.innerHeight - 视口高度(px)window.location - 当前URL信息window.document - 当前文档对象window.navigator - 浏览器/设备信息window.screen - 屏幕信息window.localStorage - 本地存储window.sessionStorage - 会话存储getElementById(id)getElementsByClassName(className)getElementsByTagName(tagName)querySelector(selector)querySelectorAll(selector)createElement(tagName)Element - DOM元素节点Text - 文本节点Document - 文档节点DocumentFragment - 文档片段href - 完整URLprotocol - 协议(http:/https:)host - 主机名和端口hostname - 主机名port - 端口pathname - 路径部分search - 查询字符串hash - 片段标识符userAgent - 用户代理字符串platform - 操作系统平台language - 浏览器语言geolocation - 地理位置APIback() - 后退forward() - 前进go(n) - 跳转到指定历史记录pushState(state, title, url) - 添加历史记录replaceState(state, title, url) - 替换当前历史记录// 添加事件监听
element.addEventListener('click', function(e) {
// 事件处理逻辑
});
// 移除事件监听
element.removeEventListener('click', handler);
setItem(key, value)getItem(key)removeItem(key)clear()fetch(url, {
method: 'GET', // 或 'POST', 'PUT' 等
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => console.log(data));
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status === 200) {
console.log(xhr.responseText);
}
};
xhr.send();
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 50, 50);
performance.now() - 高精度时间戳performance.memory - 内存使用情况(Chrome)performance.getEntries() - 资源加载性能数据navigator.geolocation.getCurrentPosition(position => {
console.log(position.coords.latitude, position.coords.longitude);
});
deviceorientation 事件devicemotion 事件// 主线程
const worker = new Worker('worker.js');
worker.postMessage(data);
worker.onmessage = e => console.log(e.data);
// worker.js
onmessage = e => {
const result = process(e.data);
postMessage(result);
};
const socket = new WebSocket('ws://example.com');
socket.onopen = () => socket.send('Hello Server!');
socket.onmessage = e => console.log(e.data);
此文档提供了浏览器环境的核心功能概述,实际实现可能因浏览器版本和供应商而有所不同。
热门推荐:
0