6.3BOM中其他常见对象

 location对象

location对象,是window对象的一个属性,代表浏览器上URL地址栏,使用location对象可以操作地址栏

 
 

<!DOCTYPE html>
<html>
        <head>
                <meta charset="UTF-8">
                <title></title>
                <script>
                        function fun1(){
                                console.log(location.host);// 服务器的IP+端口号
                                console.log(location.hostname);// IP 
                                console.log(location.port);// 端口号
                                console.log(location.href);// 地址栏中具体的文字
                        
                                location.href="https://www.baidu.com"
                        }
                        
                </script>
        </head>
        <body>
                <input type="button" value="测试location" onclick="fun1()" />
        </body>
</html>

 

history对象

history对象是window对象的一个属性,代表浏览器访问历史记录,通过history的操作我们可以实现翻阅浏览器历史网页

<!DOCTYPE html>
<html>
        <head>
                <meta charset="UTF-8">
                <title></title>
                <script>
                        function fun1(){
                                window.history.forward();
                                
                        }
                        function fun2(){
                                history.back();
                        }
                        function fun3(){
                                history.go(2); // 正整数 向前跳转 * 页  负整数 向后跳转*页
                        }
        
                        
                </script>
        </head>
        <body>
                <a href="a.html" target="_self">pageA</a>
                <input type="button" value="向前" onclick="fun1()"/>
                <input type="button" value="向后" onclick="fun2()"/>
                <input type="button" value="go" onclick="fun3()"/>
        </body>
</html>

 

 

screen和navigator
screen代表屏幕,navigator代表浏览器软件本身,通过这两个对象可以获得屏幕和浏览器软件的一些信息

 

<!DOCTYPE html>
<html>
        <head>
                <meta charset="UTF-8">
                <title></title>
                <script>
                        function fun1(){
                                console.info(window.screen.width)
                                console.info(window.screen.height)
                                console.info(navigator.userAgent)
                                console.info(navigator.appName)
                        }
                </script>
        </head>
        <body onload="fun1()">
        </body>
</html>

 

 

posted @   CodeWhisperer001  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示