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

相关推荐
小碗羊肉1 天前
【从零开始学Java | 第二十八篇】可变参数
java·开发语言
weixin199701080161 天前
《米思米商品详情页前端性能优化实战》
前端·性能优化·php
zhensherlock1 天前
Protocol Launcher 系列:Agenda 优雅组织你的想法与日程
javascript·macos·ios·typescript·node.js·mac·ipad
清汤饺子1 天前
Cursor + Claude Code 组合使用心得:我为什么不只用一个 AI 编程工具
前端·javascript·后端
weitingfu1 天前
Excel VBA 入门到精通(二):变量、数据类型与运算符
java·大数据·开发语言·学习·microsoft·excel·vba
foundbug9991 天前
无人机离散系统模型预测控制(MPC)MATLAB实现
开发语言·matlab·无人机
爱写代码的小朋友1 天前
使用 Nuitka 打包 Python 应用:从入门到进阶
开发语言·python
yuan199971 天前
C# 断点续传下载文件工具设计与实现
开发语言·c#
想唱rap1 天前
线程之条件变量和生产消费模型
java·服务器·开发语言·数据库·mysql·ubuntu
GISer_Jing1 天前
2026年前端AI开发终极指南
前端·人工智能