BOM(Browser Object Model)浏览器对象模型,就是操作浏览器的一些能力,可以操作的内容如下:
BOM的核心就是window对象,window是浏览器的一个对象,里面包含着操作浏览器的方法。
window 中有一个对象叫做history,是专门用来存储历史记录信息的,在编写时可不使用 window 这个前缀。
window.history.go(-2); // 返回上上页
window.history.go(-1); // 返回上一页
window.history.go(0); // 刷新当前页
window.history.go(1); // 前往下一页
window.history.back();
window.history.forward();
注意:pathname、search属性在HashRouter路由模式下会失效,只能在BrowserRouter路由模式下使用。
假定当前浏览器加载的URL是:http://test.csdn.com:3000/#/build_table?id=1
常用属性如下表所示:
属性 | 值 | 说明 |
---|---|---|
location.protocol | http: | 页面使用的协议,通常是http或https |
location.hostname | test.csdn.com | 服务器域名 |
location.port | 3000 | 请求的端口号 |
location.host | test.csdn.com:3000 | 服务器名及端口号 |
location.origin | http://text.csdn.com:3000 | url源地址,只读 |
location.href | 完整的URL地址 | 等价于window.location |
location.pathname | /(这里指端口号3000后面的/) | URL中的路径和文件名,不会返回hash和search后面的内容,只有当打开的页面是一个文件时才会生效 |
以下是几个特殊属性:
location.hash:URL散列值(#号之后的部分,包括#号);
location.search:URL的查询字符串(?号后面的部分,包括?,指的是端口后面紧接着的?号,而不是#号后面的?号)
- url为:http://test.csdn.com:3000/#/build_table?id=1
console.log(location.hash) // "#/build_table?id=1"
console.log(location.search ) // ""
- url为:http://test.csdn.com:3000/?id=1#/build_table
console.log(location.hash) // "#/build_table"
console.log(location.search) // "?id=1#/build_table"
设置属性,URL为:http://test.csdn.com:3000/#/build_table?id=1
location.hash = '#/cerate_table?id=2'
console.log(location.href) // http://test.csdn.com:3000/#/cerate_table?id=2
location.search= '?/id=2'
console.log(location.href) // http://test.csdn.com:3000/#/?id=2
其他属性示例:http://foouser:barpassword@www.wrox.com:80/HB/new_file2.html
属性 | 值 | 说明 |
---|---|---|
location.username | foouser | 域名前指定的用户名 |
location.password | barpassword | 域名前指定的密码 |
// 指定协议跳转
location.assign("http://www.baidu.com")
/* 等同于 */
location.href = "http://www.baidu.com"
// 以当前页面协议跳转
location.assign("//www.baidu.com")
/* 等同于 */
location.href = "//www.baidu.com"
// 重新加载,可能是从缓存加载
location.reload();
// 重新加载,从服务器加载
location.reload(true)
console.log("screenLeft 位置:", window.screenLeft);
console.log("screenTop 位置:", window.screenTop);
// 把窗口移动到左上角
window.moveTo(0,0)
// 把窗口移动到坐标位置(200,300)
window.moveTo(200,300)
// 把窗口向下移动100像素
window.moveBy(0,100)
// 把窗口向左移动10像素
window.moveBy(-10,0)
console.log("innerWidth 宽度:",window.innerWidth);
// innerWidth 宽度: 1002
console.log("innerHeight 高度:",window.innerHeight);
// innerHeight 高度: 907
console.log("outerWidth 宽度:",window.outerWidth);
// outerWidth 宽度: 1680
console.log("outerHeight 高度:",window.outerHeight);
// outerHeight 高度: 1010
window.onscroll = function(){
console.log('浏览器滚动了');
}
Safari浏览器使用的是window.pageXOffset和window.pageYOffset
console.log("scrollX 向右滚动了多少距离:",window.scrollX);
// scrollX 向右滚动了多少距离: 0
console.log("scrollY 向下滚动了多少距离:",window.scrollY);
// scrollY 向下滚动了多少距离: 0
// 滚动到页面左上角
window.scrollTo(0,0)
// 滚动到页面左边100像素和顶部200像素的位置
window.scrollTo(100,200)
// 相对于当前视口向下滚动100像素
window.scrollBy(0,100)
// 相对于当前视口向右滚动40像素
window.scrollBy(40,0)
属性 | 描述 |
---|---|
screen.height | 获取整个屏幕的高 |
screen.width | 获取整个屏幕的宽 |
screen.availHeight | 整个屏幕的高减去系统部件的高,可用屏幕的高 |
screen.availWidth | 整个屏幕的宽减去系统部件的宽,可用屏幕的宽 |
属性 | 描述 |
---|---|
navigator.userAgent | 获取浏览器的整体信息 |
navigator.appName | 获取浏览器名称 |
navigator.appVersion | 获取浏览器的版本号 |
navigator.platform | 获取当前计算机操作系统 |
参与评论
手机查看
返回顶部