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对象

相关推荐
jiangzhihao051520 分钟前
前端自动翻译插件webpack-auto-i18n-plugin的使用
前端·webpack·node.js
软件技术NINI2 小时前
html css网页制作成品——HTML+CSS盐津铺子网页设计(5页)附源码
前端·css·html
R-G-B3 小时前
【02】C#入门到精通——C# 变量、输入/输出、类型转换
开发语言·c#·c# 变量·c#输入/输出·c#类型转换
星河队长3 小时前
C# 软件加密方法,有使用时间限制,同时要防止拷贝
开发语言·c#
史迪奇_xxx3 小时前
10、一个简易 vector:C++ 模板与 STL
java·开发语言·c++
2301_801252223 小时前
Java中的反射
java·开发语言
Kiri霧3 小时前
Rust开发环境搭建
开发语言·后端·rust
weixin-a153003083163 小时前
[数据抓取-1]beautifulsoup
开发语言·python·beautifulsoup
mapbar_front4 小时前
面试问题—我的问题问完了,你还有什么想问我的吗?
前端·面试
quweiie4 小时前
thinkphp8+layui多图上传,带删除\排序功能
前端·javascript·layui