window
对象表示浏览器窗口,几乎所有的全局对象、函数和变量都是 window
对象的属性和方法。
方法/属性 | 参数说明 | 描述 | 示例代码 |
---|---|---|---|
window.alert(message) | message (字符串) | 显示带有消息和 OK 按钮的警告框 | window.alert('Hello, World!'); |
window.confirm(message) | message (字符串) | 显示带有消息以及 OK 和取消按钮的对话框 | const result = window.confirm('Are you sure?'); |
window.prompt(message, default) | message (字符串), default (字符串, 可选) | 显示可以提示用户输入的对话框 | const name = window.prompt('Enter your name:', ''); |
window.open(url, name, specs, replace) | url (字符串), name (字符串), specs (字符串), replace (布尔值) | 打开一个新的浏览器窗口或标签页 | window.open('https://www.example.com', '_blank'); |
window.close() | 无 | 关闭当前的浏览器窗口 | window.close(); |
window.setTimeout(function, delay) | function (函数), delay (毫秒) | 在指定的时间后执行函数 | window.setTimeout(() => { alert('Hello'); }, 1000); |
window.setInterval(function, delay) | function (函数), delay (毫秒) | 每隔指定的时间执行函数 | window.setInterval(() => { console.log('Tick'); }, 1000); |
window.clearTimeout(id) | id (整数) | 取消由 setTimeout 设置的定时器 | const id = setTimeout(() => {}, 1000); window.clearTimeout(id); |
window.clearInterval(id) | id (整数) | 取消由 setInterval 设置的定时器 | const id = setInterval(() => {}, 1000); window.clearInterval(id); |
window.location | 无 | 获取或设置当前窗口的地址(URL) | window.location.href = 'https://www.example.com'; |
window.history | 无 | 操作浏览器的历史记录 | window.history.back(); |
window.navigator | 无 | 获取浏览器的用户代理信息 | console.log(window.navigator.userAgent); |
window.screen | 无 | 获取用户屏幕的信息 | console.log(window.screen.width); |
location
对象表示当前文档的地址(URL),并提供了用于修改该地址的方法。
方法/属性 | 参数说明 | 描述 | 示例代码 |
---|---|---|---|
location.href | 无 | 获取或设置当前页面的 URL | console.log(location.href); |
location.protocol | 无 | 获取或设置当前 URL 的协议部分 | console.log(location.protocol); |
location.host | 无 | 获取或设置当前 URL 的主机部分 | console.log(location.host); |
location.pathname | 无 | 获取或设置当前 URL 的路径部分 | console.log(location.pathname); |
location.search | 无 | 获取或设置当前 URL 的查询部分 | console.log(location.search); |
location.hash | 无 | 获取或设置当前 URL 的片段标识符部分 | console.log(location.hash); |
location.assign(url) | url (字符串) | 加载新的文档 | location.assign('https://www.example.com'); |
location.replace(url) | url (字符串) | 替换当前文档 | location.replace('https://www.example.com'); |
location.reload() | 无 | 重新加载当前文档 | location.reload(); |
history
对象包含浏览器的历史记录。
方法 | 参数说明 | 描述 | 示例代码 |
---|---|---|---|
history.back() | 无 | 加载历史记录中的前一个 URL | history.back(); |
history.forward() | 无 | 加载历史记录中的下一个 URL | history.forward(); |
history.go(delta) | delta (整数) | 加载历史记录中的特定页面,相对当前页面的位置 | history.go(-1); |
history.pushState(state, title, url) | state (对象), title (字符串), url (字符串, 可选) | 添加一个新的历史记录条目 | history.pushState({ page: 1 }, 'title', '/page1'); |
history.replaceState(state, title, url) | state (对象), title (字符串), url (字符串, 可选) | 修改当前的历史记录条目 | history.replaceState({ page: 2 }, 'title', '/page2'); |
navigator
对象表示用户代理的状态和身份。
属性 | 参数说明 | 描述 | 示例代码 |
---|---|---|---|
navigator.userAgent | 无 | 返回当前浏览器的用户代理字符串 | console.log(navigator.userAgent); |
navigator.platform | 无 | 返回浏览器正在运行的操作系统平台 | console.log(navigator.platform); |
navigator.language | 无 | 返回用户的首选语言 | console.log(navigator.language); |
navigator.onLine | 无 | 返回一个布尔值,指示浏览器是否处于在线状态 | console.log(navigator.onLine); |
navigator.geolocation | 无 | 提供访问设备地理位置的接口 | navigator.geolocation.getCurrentPosition(position => { console.log(position); }); |
screen
对象包含有关用户屏幕的信息。
属性 | 参数说明 | 描述 | 示例代码 |
---|---|---|---|
screen.width | 无 | 返回屏幕的宽度,以像素为单位 | console.log(screen.width); |
screen.height | 无 | 返回屏幕的高度,以像素为单位 | console.log(screen.height); |
screen.availWidth | 无 | 返回屏幕的可用宽度,以像素为单位 | console.log(screen.availWidth); |
screen.availHeight | 无 | 返回屏幕的可用高度,以像素为单位 | console.log(screen.availHeight); |