如何从不同域的脚本报告 JS 错误?

发布于 2024-12-20 08:17:11 字数 1532 浏览 0 评论 0原文

我目前正在尝试使用 jquery 报告所有未捕获的 JS 错误:

window.onerror = function(msg, file, line) {
  $.post("https://"+current_url+"/js_error", {
     msg: msg
     file: file
     line: line
  });
}

current_url 始终是我当前所在的域。

当我在 www.website.com 并从 www.website.com/script.js (同一域)加载脚本时,一切正常。我尝试使用未定义的变量,并得到正确的消息变量未定义发布到https://www.website.com/js_error

现在,我有一些子域:a.website.comb.website.com等。它们都有不同的内容,但使用完全相同的javascript。

我的想法是:始终包含来自 www.website.com 的 javascript,这样当您切换子域时,脚本可以被浏览器缓存,而不必再次重新下载。

但不幸的是,这打破了上面的错误报告。当脚本包含在不同的域中时,例如我位于 a.website.com 并包含来自 www.website.com/script.js 的脚本,我只将这些错误发布到 https://a.website.com/js_error (对于 Firefox 浏览器):

"Script Error." on line 0

我意识到这是由于同源策略所致,请参阅 这个问题

我还尝试将 current_url 硬编码为 www.website.com (从加载脚本的位置),因此无论我所在的域如何,POST 总是在那里。但是这个 POST 在与 www.website.com 不同的子域中根本不起作用(我认为因为跨域 ajax POST 是不可能的)。

我还尝试将错误作为 GET ($.get) 发送,但这总是给我“脚本错误”。在子域的第 0 行 - 无论 GET 的目标是什么。

那么,如何报告我的脚本错误?我想检测它们,以便修复它们,但不想放弃缓存。

顺便说一句:我正在使用 firebug 来调试我的脚本,因此当我收到错误时我会检测到错误。但由于复杂性,并不总是能够发现每个操作系统/浏览器组合的每个错误,并且我希望确保在我的客户端发生这些错误时检测到它们。

编辑:jsfiddle 已删除

I'm currently trying to report all uncatched JS errors like this with jquery:

window.onerror = function(msg, file, line) {
  $.post("https://"+current_url+"/js_error", {
     msg: msg
     file: file
     line: line
  });
}

current_url is always the domain I am currently at.

When I am at www.website.com and the script is loaded from www.website.com/script.js (same domain), everything works fine. I tried it with a undefined variable, and get the correct message variable is undefined postet to https://www.website.com/js_error.

Now, I have some subdomains: a.website.com, b.website.com etc. All of them have different content, but use exactly the same javascript.

My idea was the following: Always include the javascript from www.website.com, so that when you switch the subdomain, the script can be cached by browsers and does not have to be redownloaded again.

But unfortunately this breaks the error reporting above. When the script is included from a different domain, e.g. I am at a.website.com and include the script from www.website.com/script.js, I only get these errors posted to https://a.website.com/js_error (for firefox browsers):

"Script Error." on line 0

I realize this is due to the same-origin policy, see this question.

I also tried to hardcode current_url to www.website.com (from where the script is loaded), so the POST goes always there regardless of the domain I am at. But this POST does not work at all from a different subdomain than www.website.com (I think because cross domain ajax POSTs are not possible).

I also tried to send the error as a GET ($.get), but this always give me "Script Error." on line 0 on a subdomain - regardless the target of the GET.

So, how can I report errors for my script? I want to detect them so I can fix them, but do not want to give up the caching.

BTW: I am using firebug to debug my scripts, so I detect errors when I get them. But due to the complexity it is not always possible to spot every error for every OS/browser combination, and I want make sure to detect them when they happen at my clients, too.

EDIT: jsfiddle deleted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2024-12-27 08:17:11

CORS 正是您的问题,只需将 CORS 允许的标头添加到您的 https://"+current_url+"/js_error 响应中即可。

Access-Control-Allow-Origin: *

或者更加严格。

Access-Control-Allow-Origin: http://a.website.com http://b.website.com

http://enable-cors.org/ 是一个很好的资源。

至于 window.onerror 没有为您提供来自不同域的所有有错误的数据;
我可以确认这不是你疯了,这就是 Firefox 的运作方式。
http://jsbin.com/ojavoy/3/edit#javascript,html

  • Firefox 忽略了行号
  • Chrome 没有提供脚本名称或行号
  • IE <8 不支持 window.onerror(不知道 9)

我见过有人尝试包装他们的脚本{} catch{} 块。这将适用于跨浏览器,并且会捕获一些但不是全部错误(这比 IE 中没有错误要好)。目前没有“正确的方法来做到这一点”。

CORS is exactly your problem, just add the CORS allowed headers to your https://"+current_url+"/js_error response.

Access-Control-Allow-Origin: *

Or to be more restrictive.

Access-Control-Allow-Origin: http://a.website.com http://b.website.com

http://enable-cors.org/ is a good resource for this.

As for the window.onerror not giving you all the data with errors from separate domains;
I can confirm this isn't you being crazy and it's how firefox operates.
http://jsbin.com/ojavoy/3/edit#javascript,html

  • Firefox leaves out the line number
  • Chrome doesn't give the script name or line number
  • IE <8 doesn't support window.onerror (don't know about 9)

I've seen people wrap their scripts in a bit try{}catch{} block. That will work cross browsers and will catch some but not all errors (which is better then none in IE). There currently isn't a "right way to do this".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文