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这里面用

就可以写到客户端那边

3. 只要在我们的这个浏览器不同页面都共用同一个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对象 

相关推荐

  1. C++ 入门13异常处理

    2023-12-07 06:22:02       19 阅读
  2. 15异常处理

    2023-12-07 06:22:02       56 阅读
  3. 10-异常处理

    2023-12-07 06:22:02       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-07 06:22:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-07 06:22:02       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-07 06:22:02       82 阅读
  4. Python语言-面向对象

    2023-12-07 06:22:02       91 阅读

热门阅读

  1. 【AI-ChatGPT-Prompt】什么是Prompt

    2023-12-07 06:22:02       54 阅读
  2. 一些深度语音预处理小细节

    2023-12-07 06:22:02       59 阅读
  3. 车联网之电子围栏模型使用翻滚窗口【二十一】

    2023-12-07 06:22:02       61 阅读
  4. k8s 中externalTrafficPolicy应用场景和实践

    2023-12-07 06:22:02       60 阅读