HtML之JavaScript BOM编程

HtML之JavaScript BOM编程

window

  • history 历史

  • location 地址栏

  • document 浏览器打开的.html文档

  • console F12开发者工具的控制台

  • screen 屏幕

  • navigator 浏览器软件本身(历史原因一直沿用)

  • sessionStorage 会话级存储

  • localStorage 持久级存储
    window对象 API

  • window对象的属性 API

  • history 窗口的访问历史

  • location 地址栏

  • sessionStorage 用于存储一些会话级数据(浏览器关闭,数据清除)

  • localStorage 用于存储一些持久级数据(浏览器关闭数据存在)

  • console log

通过window对象及其属性的API控制浏览器的属性和行为

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        /*
            window 对象是由浏览器提供使用的,无需自己创建(window. 可省略)
                三种弹窗
                 alert
                 prompt
                 confirm

            定时任务
        */
        function fun1() {
            window.alert("信息提示")
        }
        function fun1() {
            window.prompt("信息输入")
        }
        function fun1() {
            window.confirm("信息确认")
        }
        function fun4() {
            window.setTimeout(function () {
                alert("两秒")
            }, 2000)
        }
    </script>
    <script>
        /*
            window属性的API
                history 窗口的访问历史
                location 地址栏
                sessionStorage 用于存储一些会话级数据(浏览器关闭,数据清除)
                localStorage   用于存储一些持久级数据(浏览器关闭数据存在)
                console  log

        */
        function funA() {
            history.forward() // 向前翻页
            // history.go(n)  正数表示前进,负数表示后退 n表示页数
        }
        function funB() {
            history.back() // 向后翻页
        }
        function funcLocation() {
            location.href = "http://www.baidu.com"  // 修改地址栏中的url并跳转
        }
    </script>
    <script>
        function funcSessionStorage1() {
            sessionStorage.setItem("A", 1212)   //类似map是key value格式
            localStorage.setItem("A1", 1212)   //类似map是key value格式
        }
        function funcSessionStorage2() {
            var aaa = sessionStorage.getItem("A")
            var bbb = localStorage.getItem("A1")
            alert(aaa + bbb)
        }
        function funcSessionStorage3() {
            // sessionStorage.removeItem()
            sessionStorage.clear()
            localStorage.clear()
        }
    </script>
</head>

<body>
    <button onclick="fun1()">信息提示</button>
    <button onclick="fun2()">信息输入</button>
    <button onclick="fun3()">信息确认</button>
    <button onclick="fun4()">两秒后提示</button>

    <hr>
    <a href="https://www.baidu.com">百度</a> <!--需要带https 不然会识别为本地文件-->
    <button onclick="funA()">上一页</button>
    <button onclick="funB()">下一页</button>

    <button onclick="funcLocation()">测试Location</button>
    <hr>
    <button onclick="funcSessionStorage1()">测试sessionStorage存储</button>
    <button onclick="funcSessionStorage2()">测试sessionStorage读取</button>
    <button onclick="funcSessionStorage3()">测试sessionStorage清空</button>
</body>

</html>
相关推荐
恋猫de小郭2 小时前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter
崔庆才丨静觅8 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60619 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了9 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅9 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅10 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅10 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment10 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅10 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊10 小时前
jwt介绍
前端