window.addEventListener('load', () => { console.log('load'); })
window.onload = function () { //... ... }
window.addEventListener('DOMContentLoaded', () => { //... ... })
window.addEventListener('beforeunload', (event) => { event.returnValue = '您确定要离开此页面吗?'; console.log('beforeunload'); });
window.addEventListener('resize', () => { //... ... })
let setFontSize = () => { let w = document.documentElement.clientWidth; let size = (20 * (w / 320) > 40 ? 40 + 'px' : (20 * (w / 320) + 'px')) document.documentElement.style.fontSize = size; } window.addEventListener('load', setFontSize) window.addEventListener('resize', setFontSize)
window.addEventListener('online', () => { console.log('online'); }) window.addEventListener('offline', () => { console.log('offline'); })
.针对窗口的操作,不限于文档内容外,还有其它内容,如复制 URL,涵盖面更广
.事件的原始目标是 元素 Element,可以在捕获或冒泡阶段响应并处理
window.addEventListener('copy', (e) => { e.preventDefault() }) window.addEventListener('paste', (e) => { e.preventDefault() })