Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save huixisheng/25446a83516b20d3fb07 to your computer and use it in GitHub Desktop.
Save huixisheng/25446a83516b20d3fb07 to your computer and use it in GitHub Desktop.

捕捉浏览器中的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 )

Chrome (23.0.1271.101)


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)

Firefox (16.0.2)


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)

Opera (12.11)


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)

IE9


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)

IE8


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)
-

IE7


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)
-

IE6


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)
-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment