javascript异常档案:http://errors.totorojs.org/wiki/
Language | Message | Browser | 备注 |
---|---|---|---|
英文 | Script error. | Firefox, Chrome, Safari, Opera. | |
Script Error: 0x80072ee4 |
异常信息通常如下:
异常字段 | 值 |
---|---|
Message | Script error. |
File | (空) |
Line | 0 |
javascript:notExistFunction()
代码,Firebug 可以提示正确的"notExistFunction" is undefined
, 但是 onerror 异常事件处理函数中得到的错误消息却是 Script error.
。Script error.
。
通常大型网站会将静态资源使用独立域名进行管理,这样可以有以下优点:
但是这样就会带来异常捕获与分析的麻烦。解决办法有:
Access-Control-Allow-Origin
头信息。script
标签添加 crossorigin
属性。a.com/a.html
<script type="text/javascript"> window.onerror = function(m,f,l){ var s = "message:"+m+", file:"+f+", line:"+l; console.log(s); window.status = s; }; </script> <script type="text/javascript" src="b.com/b.html"></script>
这里 a.com/a.html 页面引用了 b.com/b.html 脚本『注意:b.html 是一个页面』, 这时会抛出异常:
Browser | type | Message | File | Line |
---|---|---|---|---|
IE | console | 语法错误 | b.com/b.html | 1 |
onerror | 语法错误 | b.com/b.html | 1 | |
Firefox | console | syntax error | b.com/b.html | 1 |
onerror | Script error. | 0 | ||
Chrome | console | Uncaught SyntaxError: Unexpected token < | b.com/b.html | 1 |
onerror | Script error. | 0 | ||
Safari | console | |||
onerror | Script error. | 0 | ||
Opera | console | Syntax error at line 1 while loading: expected expression, got ‘<‘ <!DOCTYPE html PUBLI ^ |
a.com/a.html | 4 |
onerror | Script error. | 0 |
引入外部脚本,该外部脚本会抛出异常:
a.com/a.html
<script type="text/javascript"> window.onerror = function(m,f,l){ var s = "message:"+m+", file:"+f+", line:"+l; console.log(s); window.status = s; }; </script> <script type="text/javascript" src="b.com/b.js"></script>
b.com/b.js
// some comments // for lines. var a = ;
Browser | type | Message | File | Line |
---|---|---|---|---|
IE | console | 语法错误 | b.com/b.js | 3 |
onerror | 语法错误 | b.com/b.js | 3 | |
Firefox | console | syntax error | b.com/b.js | 3 |
onerror | Script error. | 0 | ||
Chrome | console | Uncaught SyntaxError: Unexpected token ; | b.com/b.js | 3 |
onerror | Script error. | 0 | ||
Safari | console | Script error. | 0 | |
onerror | Script error. | 0 | ||
Opera | console | Syntax error at line 3 while loading: expected expression, got ‘;’ var a = ; ——–^ |
a.com/a.html | 4 |
onerror | Script error. | 0 |
特殊的,如果是脚本中主动掷出的异常,Safari 会稍有不同。
b.com/b.js
// some comments // for lines. throw new Error("test error on b.");
Browser | type | Message | File | Line |
---|---|---|---|---|
IE | console | test error on b. | b.com/b.js | 3 |
onerror | test error on b. | b.com/b.js | 3 | |
Firefox | console | test error on b. | b.com/b.js | 3 |
onerror | Script error. | 0 | ||
Chrome | console | Uncaught Error: test error on b. | b.com/b.js | 3 |
onerror | Script error. | 0 | ||
Safari | console | Error: test error on b. | ||
onerror | undefined | 0 | ||
Opera | console | Uncaught exception: Error: test error on b. | a.com/a.html | 4 |
onerror | Script error. | 0 |
参考自:javascript异常档案