JS BOM API集合

2024-07-20 14:49:00 194
浏览器对象模型(BOM,Browser Object Model)是指浏览器中提供的对象和方法的集合,用于与浏览器窗口进行交互。BOM 包括 `window`、`document`、`navigator`、`screen`、`location` 和 `history` 等对象。以下是详细的 BOM API 列表及其说明。

Window 对象

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 对象

location 对象表示当前文档的地址(URL),并提供了用于修改该地址的方法。

方法/属性参数说明描述示例代码
location.href获取或设置当前页面的 URLconsole.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 对象包含浏览器的历史记录。

方法参数说明描述示例代码
history.back()加载历史记录中的前一个 URLhistory.back();
history.forward()加载历史记录中的下一个 URLhistory.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 对象表示用户代理的状态和身份。

属性参数说明描述示例代码
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 对象包含有关用户屏幕的信息。

属性参数说明描述示例代码
screen.width返回屏幕的宽度,以像素为单位console.log(screen.width);
screen.height返回屏幕的高度,以像素为单位console.log(screen.height);
screen.availWidth返回屏幕的可用宽度,以像素为单位console.log(screen.availWidth);
screen.availHeight返回屏幕的可用高度,以像素为单位console.log(screen.availHeight);