首页 > 解决方案 > 没有 `crossorigin` 的脚本不会暴露它的错误?

问题描述

这是我的文件

crossorigintest
├── 1.html
└── 1.js

文件内容是

1.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        window.onerror = function () {
            console.log('onerror', arguments)
        }
        window.addEventListener('error', function () {
            console.log('addEventListenerError', arguments)
        }, true)
        window.addEventListener('unhandledrejection', function (event) {
            console.log('unhandledrejection', arguments)
            event.preventDefault();
        });

    </script>
    <script src="http://localhost:8081/1.js"></script>
</body>

</html>

1.js

console.log(11);
(function(){
    console.log(22);
    throw new Error('oops')
})()

启动http服务器

cd crossorigintest
npx http-server
npx http-server --cors

然后文件将被代理,http://localhost:8080并且http://localhost:8081

打开浏览器

http://localhost:8080/1.html

看结果

在此处输入图像描述

实际上错误暴露在html之外,那么规范是什么意思? https://html.spec.whatwg.org/multipage/scripting.html#attr-script-crossorigin

crossorigin 属性是 CORS 设置属性。对于经典脚本,它控制从其他来源获取脚本时是否会暴露错误信息。

标签: javascripthtmlgoogle-chromecross-domain

解决方案


根据我的测试,这取决于我的 chrome 的隐私设置。When the third option is selected, the spec works. 在此处输入图像描述


推荐阅读