JS核心——BOM

Window

window对象的理解: 全局变量均自动成为window对象的属性 全局函数均自动成为window对象的函数

Location

   <body>
        
        <button onclick="fun01()">跳转页面</button>
        <button onclick="fun02()">刷新页面</button>
        
        <script type="text/javascript">
​
            function fun01(){
                //window.location.href = "http://www.baidu.com";
                //location.href = "http://www.baidu.com";
                location = "http://www.baidu.com";
            }
            
            function fun02(){
                window.location.reload();
            }
        </script>
        
    </body>

History

    <body>
        
        <button onclick="fun01()">上两页</button>
        <button onclick="fun02()">上一页</button>
        <button onclick="fun03()">刷新</button>
        <button onclick="fun04()">下一页</button>
        <button onclick="fun05()">下两页</button>
        
        <script type="text/javascript">
            
            function fun01(){
                window.history.go(-2);
            }
            
            function fun02(){
//              window.history.go(-1);
                window.history.back();
            }
            
            function fun03(){
                window.history.go(0);
            }
            
            function fun04(){
//              window.history.go(1);
                window.history.forward();
            }
            
            function fun05(){
                window.history.go(2);
            }
            
        </script>
        
    </body>

open

    <body>
        
        <h1>一定不要点这里</h1>
        
        <script type="text/javascript">
            window.open("http://www.baidu.com","baidu");
        </script>
        
    </body>

各种弹框

    <body>
        
        <button onclick="fun01()">警告框</button>
        <button onclick="fun02()">确认框</button>
        <button onclick="fun03()">提示框</button>
        
        <script type="text/javascript">
            
            function fun01(){
                alert("警告框");
            }
            
            function fun02(){
                //点击确认 -- 返回true
                //点击取消 -- 返回false
                var bool = confirm("确认框");
                console.log(bool);
            }
            
            function fun03(){
                //点击确认 -- 返回输入的数据
                //点击取消 -- 返回null
                var v = prompt("提示框","默认值");
                console.log(v);
            }
        </script>
    </body>

定时器

<body>
        
        <button onclick="fun()">取消定时器</button>
        <br />
        <img src="../img/王也02.jpg" width="100px" height="100px"/>
        
        <script type="text/javascript">
            
            var img = document.getElementsByTagName("img")[0];
            
            //5000毫秒后触发定时器内的函数
            var timeout = setTimeout(function(){
                img.setAttribute("src","../img/王也03.jpeg");
                img.setAttribute("width","200px");
                img.setAttribute("height","200px");
            },5000);
            
            
            
            function fun(){
                clearTimeout(timeout);
            }
            
        </script>
        
    </body>

Cookie

/* * 知识点: * 理解:以键值对的形式存储在当前浏览器下的纯文本数据 * * 注意: * 1.不同的浏览器不能共享Cookie里的数据 * 2.同一个浏览器下,不同域名不能共享Cookie里的数据 * 3.同一个浏览器下,同一个域名可以共享该域名下的Cookie里的数据 * * 缺点: * 1.浏览器可以禁用该域名下的Cookie * 2.Cookie其实是该浏览器下的一个文件,可能会被误删 * 3.以键值对形式存储的纯文本数据,可能存在解密的风险 */

    <body>
        
        <button onclick="fun01()">添加Cookie</button>
        <button onclick="fun02()">删除Cookie</button>
        <button onclick="fun03()">修改Cookie</button>
        <button onclick="fun04()">查询Cookie</button>
        
        <script type="text/javascript">
​
            
            function fun01(){
                var date = new Date();
                date.setTime(date.getTime() + 1000*60*60*24*10);
                document.cookie = "username=hhy;expires=" + date.toGMTString();
                document.cookie = "password=123123;expires=" + date.toGMTString();
            }
            
            function fun02(){
                var date = new Date();
                date.setTime(date.getTime());
                document.cookie = "username=hhy;expires=" + date.toGMTString();
                document.cookie = "password=123123;expires=" + date.toGMTString();
            }
            
            function fun03(){
                var date = new Date();
                date.setTime(date.getTime() + 1000*60*60*24*10);
                document.cookie = "username=xiaohong;expires=" + date.toGMTString();
                document.cookie = "password=123456;expires=" + date.toGMTString();
            }
            
            function fun04(){
                alert(document.cookie);
            }
            
        </script>
        
    </body>

JS对象

       <script type="text/javascript">
            //创建学生对象
            var stu = {
                "name":"薛之谦",
                "age":40,
                "sing":function(){
                    console.log(this.name + "一展歌喉");
                },
                "sleep":function(){
                    console.log(this.name + "长睡不起");
                }
            }
            
            //调用函数
            stu.sing();
            stu.sleep();
            
            //遍历对象
            //注意:可以使用for-in遍历对象里所有的属性和函数
            for(var v in stu){
                console.log(v);
            }
            
        </script>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值