“脚本错误".仅在Safari中window.onerror中的错误 [英] "Script Error." errors in window.onerror in Safari only

查看:149
本文介绍了“脚本错误".仅在Safari中window.onerror中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到脚本错误".在window.onerror中捕获错误时,即使正确(我认为)在S3上配置了CORS标头也是如此.

CORS配置:

 <?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
 

HTML:

 <script crossorigin src="https://s3.amazonaws.com/safari-script-error/foo.js" />
 

其中包含:

 window.addEventListener("keydown", function(event) {
  if (event.keyCode === 69) { // "e" button
    throw new Error("Oh shoot");
  }
});
 

JS:

 window.onerror = function(event) {
  console.log(event);
}
 

Codepen: https://codepen.io/astashov/pen/yoEvRB

它在Chrome,Firefox和IE11中正常工作,并且仅显示脚本错误".在Safari中(我的版本为10.0.3(12602.4.8)).

如何使其也可以在Safari中使用?

解决方案

如何使其也可以在Safari中使用?

不能.当前版本的Safari不支持在跨域情况下向onerror回调提供错误消息-即使在script元素上指定了crossorigin属性.

Safari以前确实支持它,但后来又退步了.

https://bugs.webkit.org/show_bug中有一个打开的错误.cgi?id = 132945

I'm getting "Script Error." when catching errors in window.onerror, even with properly (I think) configured CORS headers on S3.

CORS config:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

HTML:

<script crossorigin src="https://s3.amazonaws.com/safari-script-error/foo.js" />

which contains:

window.addEventListener("keydown", function(event) {
  if (event.keyCode === 69) { // "e" button
    throw new Error("Oh shoot");
  }
});

JS:

window.onerror = function(event) {
  console.log(event);
}

Codepen: https://codepen.io/astashov/pen/yoEvRB

It works fine in Chrome, Firefox and IE11, and only shows "Script error." in Safari (I have Version 10.0.3 (12602.4.8)).

How to make it work in Safari too?

解决方案

How to make it work in Safari too?

You can’t. Current versions of Safari don’t support giving error messages to the onerror callback in the cross-origin case—even if the crossorigin attribute is specified on the script element.

Safari did support it previously, but subsequently regressed at some point.

There’s an open bug for this at https://bugs.webkit.org/show_bug.cgi?id=132945

这篇关于“脚本错误".仅在Safari中window.onerror中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆