H5ke13-1浏览器处理异常

复制代码
window对应的error没有event对象
window对应的error他接收三个参数,msg,url,行号

return false

return true

1就不会返回错误

复制代码
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>

        <script>
            // window.addEventListener("error",funtion(msg,url,line){
                window.onerror=function(msg,url,line){
                    console.log(msg);
                    console.log("=========");
                    console.log(url);
                    console.log("=========");
                    console.log(line);

            return false;
                    // return true;// 或者return一个true异步给服务器,这样用户就看不到错误信息了

            };

            let div=document.getElementById("div#out");
            div.innerHTML="Hello World";
        </script>

    </body>
</html>

看到我们的错误类型

2error只能在try,catch这里面用

复制代码
就可以写到客户端那边
  1. 只要在我们的这个浏览器不同页面都共用同一个window
复制代码
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <a href="b.html">链接到b</a>
        <input type="button" value="print">
        <script>
            window.name = "zhangsan";
            let btn = document.querySelector("input");
            btn.addEventListener("click", (event) => {
                alert(window.name);
                window.name = "lisi";
            })
        </script>

    </body>
</html>
复制代码
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <a href="a.html">链接到a</a>
        <input type="button" value="print">
        <script>
            let btn=document.querySelector("input");
            btn.addEventListener("click",(event)=>{
                alert(window.name);
            })
        </script>

    </body>
</html>

嘿嘿我打印JSON字符串,JSON.stringify(data)

复制代码
<body>
    <a href="b.html">链接到b</a>
    <input type="button" value="print">
    <script>
        let data={
            name:"zhangsan",
            age:18,
            gender:"female"
        }
        window.name = JSON.stringify(data);
        let btn = document.querySelector("input");
        btn.addEventListener("click", (event) => {
            alert(window.name);
            window.name = "lisi";
        })
    </script>

</body>

当然JSON.parse(window.name)可以转为JS对象

相关推荐
十年Java程序媛4 小时前
JVM 基础与 GC 垃圾回收详解|可达性分析、分代模型、垃圾收集器、STW
java·开发语言·jvm·spring boot·后端·面试
估值探索者4 小时前
【Python量化选股流水线 #03】候选股怎么排序?动量+低波+流动性三因子打分取 Top-N
开发语言·python·接口·api接口·股票数据接口·数据接口·股票数据api接口
骑士雄师4 小时前
supervisor 中关于上下文多轮信息维护常用到的python语法
开发语言·python
曹牧5 小时前
C#:字符串占位符与值数量不一致
开发语言·数据库·c#
狂师5 小时前
太强了!GitHub上开源了一款AI自动化测试框架,0代码搞定前后端测试!
前端·后端·测试
只睡四小时5 小时前
Local-First 数据契约:一张表装下 4 类数据
前端·javascript·架构设计·local-first·数据契约
迅猛龙办公室5 小时前
Python函数实现反转一个只有3位数的整数
开发语言·前端·python
我星期八休息5 小时前
Redis 的原理和使用(2)
前端·数据库·redis·python·mysql·缓存·bootstrap
Java后端的Ai之路5 小时前
19-ReactJS最佳实践
前端·react.js·前端框架
“AI国潮设计-小江”5 小时前
《【AIGC实战】Python+SDXL批量生成“英歌舞树莓香草布丁”IP,附保持角色一致性的代码逻辑》
开发语言·人工智能·python·prompt·aigc