前端BOM常用操作

BOM操作常用命令详解及代码案例

BOM(Browser Object Model)是浏览器对象模型,是浏览器提供的JavaScript操作浏览器的API。BOM提供了与网页无关的浏览器的功能对象,虽然没有正式的标准,但现代浏览器已经几乎实现了JavaScript交互性方面的相同方法和属性,因此常被认为是BOM的方法和属性。本文将详细介绍BOM操作中的常用命令,并通过代码案例进行解释。

1. 获取浏览器窗口尺寸
  • 获取可视窗口宽度 :window.innerWidth
  • 获取可视窗口高度 :window.innerHeight
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var m1 = window.innerWidth;
        var m2 = window.innerHeight;
        console.log(m1);
        console.log(m2);
    </script>
</body>
</html>
2. 浏览器的弹出层
  • 提示框 :window.alert('提示信息')
  • 询问框 :window.confirm('提示信息')
  • 输入框 :window.prompt('提示信息', '默认值')
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // window.alert('你好!')
        // var res = window.confirm('你好吗?')
        // console.log(res)
        var res2 = window.prompt('你是哪个省的?');
        console.log(res2);
    </script>
</body>
</html>
3. 开启和关闭标签页
  • 开启 :window.open('地址')
  • 关闭 :window.close()
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="on">开启</button>
    <button id="off">关闭</button>
    <script>
        var on = document.getElementById('on');
        var off = document.getElementById('off');
        on.onclick = function() {
            window.open('https://www.baidu.com/');
        }
        off.onclick = function() {
            window.close();
        }
    </script>
</body>
</html>
4. 浏览器常见事件
  • 资源加载完毕 :window.onload = function() {}
  • 可视尺寸改变 :window.onresize = function() {}
  • 滚动条位置改变 :window.onscroll = function() {}
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <img src="图片链接" alt="">
    <script>
        window.onload = function() {
            console.log('资源已经加载完毕');
        }
        window.onresize = function() {
            console.log('可视尺寸改变');
        }
        window.onscroll = function() {
            console.log('滚动条位置改变');
        }
    </script>
</body>
</html>
5. 浏览器的历史记录操作
  • 回退页面 :window.history.back()
  • 前进页面 :window.history.forward()
  • 跳转到指定页面 :window.history.go(n),其中n可以是负数(表示后退)或正数(表示前进)
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button onclick="goBack()">回退</button>
    <button onclick="goForward()">前进</button>
    <button onclick="goToPage(-2)">回退两页</button>
    <script>
        function goBack() {
            window.history.back();
        }
        function goForward() {
            window.history.forward();
        }
        function goToPage(n) {
            window.history.go(n);
        }
    </script>
</body>
</html>
6. 浏览器卷去的尺寸和滚动
  • 卷去的高度 :document.documentElement.scrollTop 或 window.scrollY
  • 卷去的宽度 :document.documentElement.scrollLeft 或 window.scrollX
  • 滚动到指定位置 :window.scrollTo(left, top) 或 window.scrollTo({left: xx, top: yy, behavior: 'smooth'})
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            width: auto;
            height: 3000px;
        }
        button {
            position: fixed;
            bottom: 50px;
            right: 50px;
        }
    </style>
</head>
<body>
    <button id="go">传送</button>
    <script>
        var go = document.getElementById('go');
        go.onclick = function() {
            window.scrollTo({left: 300, top: 400, behavior: "smooth"});
        }
    </script>
</body>
</html>

Navigator对象包含有关浏览器的信息。

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        console.log('浏览器品牌', navigator.appName);
        console.log('浏览器版本', navigator.appVersion);
        console.log('用户代理', navigator.userAgent);
        console.log('操作系统', navigator.platform);
    </script>
</body>
</html>
相关推荐
软件技术NINI14 分钟前
圣罗兰彩妆html+css 4页
前端·css·html
2601_9516152044 分钟前
网站模板 网站网页模板下载
前端·网络·html
酷炫码神1 小时前
Day 1|HTML5:一个商城首页的骨架,从 DOCTYPE 到语义化
前端·html·html5
AI源码2 小时前
鲜花商城(HTML 版)
前端·html
hiahiahia1232 小时前
Message 和对话
服务器·前端·数据库
溪语流沙2 小时前
【Web全栈进阶】Composition API与组件通信:props深入、emit、插槽
前端·css·vue.js
ZoeLandia2 小时前
TypeScript 03:泛型与工具类型
前端·typescript
code_Bo2 小时前
从策划到上线:我和 AI 一起做了个微信记账小程序
前端·ai编程·cursor
Shirley~~3 小时前
Three.js加载Gltf模型并播放动画
前端·3d
Data analyse4563 小时前
Session超时与浏览器休眠的计时校验
大数据·前端·数据库·数据分析