javascript - 没有 `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 设置属性。对于经典脚本,它控制从其他来源获取脚本时是否会暴露错误信息。
解决方案
推荐阅读
- php - 问题 SQLSTATE[HY000]: 一般错误:1215 无法添加外键约束
- html - 谷歌文本到语音 api html GET 请求中的多个参数
- database - 如果我想使用 Amazon DynamoDB 作为我的移动应用程序的数据库,我可以选择哪个 Ec2 实例?
- python - 理解尝试跳过循环的 N 次迭代时的错误
- r - 如何在 R 中使用多个数据框绘制分组条形图?
- java - 为什么我们不能通过java中的getter实现深度克隆为什么我们需要带有Cloneable接口的原型设计模式?
- powershell - 如何更改多个 .txt 文件中的字符并保存/覆盖 Powershell 中的现有文件
- linux - 删除警告:二进制文件拱门(1)不匹配包拱门(2)
- python - 如何修复我的消息加密/解密程序?
- flutter - 在颤动中单击添加按钮时,我想重新加载同一页面