当前位置: 代码网 > it编程>编程语言>Javascript > 前端BOM操作常用命令详解及代码案例

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

2024年11月26日 Javascript 我要评论
前言bom(browser object model)是浏览器对象模型,是浏览器提供的javascript操作浏览器的api。bom提供了与网页无关的浏览器的功能对象,虽然没有正式的标准,但现代浏览器

前言

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

1. 获取浏览器窗口尺寸

  • 获取可视窗口宽度window.innerwidth
  • 获取可视窗口高度window.innerheight
<!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('提示信息', '默认值')
<!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()
<!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() {}
<!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可以是负数(表示后退)或正数(表示前进)
<!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'})
<!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>

7. navigator对象

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

<!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>

总结 

到此这篇关于前端bom操作常用命令详解及代码案例的文章就介绍到这了,更多相关前端bom常用操作内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com