捕捉浏览器中的JS运行时错误,主要通过监听window.onerror来实现。但是对于不同的脚本执行方式以及不同的浏览器,能捕获到的信息会有区别。
window.onerror 讲接收3个参数:
msg
:错误描述,比如:a is not defined
url
:出错脚本所在的url
lineNumber
:出错脚本的行数
本文将对不同浏览器和不同的脚本执行方式进行测试,并总结这些区别。
首先对于脚本的执行主要有下面几种:
页面内嵌的<script>
,需要执行的代码在<script>
标签内
使用<script src="external.js">
的方式引入外部脚本,脚本为同域地址
使用<script src="external.js">
的方式引入外部脚本,脚本为不同域地址
使用<script src="external.js">
的方式引入外部脚本,脚本为本地地址
使用eval方法来执行脚本
动态地创建内嵌的<script>
并设置其innerHTML为需要执行的代码
下面列一下各浏览器对与上面集中脚本执行的捕获情况(Markdown对于table的支持不是很好,我是直接在页面上copy HTML进来的,维护可以看下 https://www.evernote.com/shard/s43/sh/a36fc90e-9f72-43b5-8710-1476340d14ac/1160f515582d297f22f495924ba10d3d )
page script
external script ( same origin )
external script ( cross domain )
external script ( local )
eval
dynamic page script
msg
✓
✓
✗(only Script error)
✗(only Script error)
✓
✓
url
✓(current page)
✓
✗("")
✗("")
✓(current page)
✓(current page)
lineNumber
✓(from current page)
✓
✗(0)
✗(0)
✓(from code)
✓(from its code)
page script
external script ( same origin )
external script ( cross domain )
external script ( local )
eval
dynamic page script
msg
✓
✓
✗(only Script error)
✗(only Script error)
✓
✓
url
✓(current page)
✓
✓
✓
✓(file that call this eval)
✓(current page)
lineNumber
✓(from current page))
✓
✗(0)
✗(0)
✓(position that calls this eval)
✓(from its code)
Safari (6.0.2 (8536.26.17))
page script
external script ( same origin )
external script ( cross domain )
external script ( local )
eval
dynamic page script
msg
✓
✓
✗(only Script error)
✓
✓
✓
url
✓(current page)
✓
✓
✓
✗(undefined)
✓(current page)
lineNumber
✓(from current page)
✓
✗(0)
✓
✓(from code)
✓(from its code)
page script
external script ( same origin )
external script ( cross domain )
external script ( local )
eval
dynamic page script
msg
✓
✓
✗(only Script error)
-
✓
✓
url
✓(current page)
✓
✗("")
-
✗("")
✓(current page)
lineNumber
✓(from current page)
✓
✗(0)
-
✓(from code)
✓(from its code)
page script
external script ( same origin )
external script ( cross domain )
external script ( local )
eval
dynamic page script
msg
✓
✓
✓
✓
✓
✓
url
✓(current page)
✓
✓
✓
✓(file path that call this eval)
✓(current page)
lineNumber
✓(from current page)
✓
✓
✓
✓(position that calls this eval)
✓(from its code)
page script
external script ( same origin )
external script ( cross domain )
external script ( local )
eval
dynamic page script(not available)
msg
✓
✓
✓
✓
✓
-
url
✓(current page)
✓
✓
✓
✓(file path that call this eval)
-
lineNumber
✓(from current page)
✓
✓
✓
✓(position that calls this eval)
-
page script
external script ( same origin )
external script ( cross domain )
external script ( local )
eval
dynamic page script(not available)
msg
✓
✓
✓
✓
✓
-
url
✓(current page)
✓
✓
✓
✓(file path that call this eval)
-
lineNumber
✓(from current page)
✓
✓
✓
✓(position that calls this eval)
-
page script
external script ( same origin )
external script ( cross domain )
external script ( local )
eval
dynamic page script(not available)
msg
✓
✓
✓
✓
✓
-
url
✓(current page)
✓(current page)
✓(current page)
✓(current page)
✓(current page)
-
lineNumber
✓(from current page)
✓(line number start from 1)
✓(line number start from 1)
✓(line number start from 1)
✓(position that calls this eval,line number start from 1)
-
跨域解决方是有:前端script 要增加crossorigin属性(只兼容谷歌 火狐等现代浏览器,低版本ie好像没跨域问题),服务器那边要运行Access-Control-Allow-Origin:*