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

相关推荐
Mr_Mao16 分钟前
Naive Ultra:中后台 Naive UI 增强组件库
前端
前端小趴菜052 小时前
React-React.memo-props比较机制
前端·javascript·react.js
摸鱼仙人~3 小时前
styled-components:现代React样式解决方案
前端·react.js·前端框架
sasaraku.3 小时前
serviceWorker缓存资源
前端
iCxhust4 小时前
c# U盘映像生成工具
开发语言·单片机·c#
RadiumAg4 小时前
记一道有趣的面试题
前端·javascript
yangzhi_emo4 小时前
ES6笔记2
开发语言·前端·javascript
yanlele5 小时前
我用爬虫抓取了 25 年 5 月掘金热门面试文章
前端·javascript·面试
emplace_back5 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk5 小时前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习