PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/content/wiki/script-error.md

https://github.com/ilife5/javascript-exception-archives
Markdown | 129 lines | 100 code | 29 blank | 0 comment | 0 complexity | 882f1085f12f96f60652c1a1c802c196 MD5 | raw file
  1. # Script error.
  2. - tags: 可排除
  3. ----
  4. | Language | Message | Browser | 备注 |
  5. |----------|---------------|---------------------------------|------|
  6. | 英文 | Script error. | Firefox, Chrome, Safari, Opera. | |
  7. ## 分析
  8. 异常信息通常如下
  9. | 异常字段 | |
  10. |----------|---------------|
  11. | Message | Script error. |
  12. | File | () |
  13. | Line | 0 |
  14. 1. 在某些版本的 Firefox 地址栏执行脚本 `javascript:notExistFunction()`
  15. 代码Firebug 可以提示正确的 `"notExistFunction" is undefined`
  16. 但是 onerror 异常事件处理函数中得到的错误消息却是 `Script error.`
  17. 1. 用户脚本(userscript) 异常导致
  18. 1. 因为同源策略Firefox, Chrome, Safari 等浏览器
  19. 页面引用的非同域的外部脚本中抛出了异常本页面无权限获得这个异常详情
  20. 所以就成了 `Script error.`静态脚本使用独立域名未必就一定是件好事啊
  21. ## 案例
  22. a.com/a.html
  23. ```html
  24. <script type="text/javascript">
  25. window.onerror = function(m,f,l){
  26. var s = "message:"+m+", file:"+f+", line:"+l;
  27. console.log(s);
  28. window.status = s;
  29. };
  30. </script>
  31. <script type="text/javascript" src="b.com/b.html"></script>
  32. ```
  33. 这里 a.com/a.html 页面引用了 b.com/b.html 脚本注意b.html 是一个页面
  34. 这时会抛出异常
  35. | Browser | type | Message | File | Line |
  36. |---------|---------|------------------------------------------------------------------------------------------------------|--------------|------|
  37. | IE | console | 语法错误 | b.com/b.html | 1 |
  38. | | onerror | 语法错误 | b.com/b.html | 1 |
  39. | Firefox | console | syntax error | b.com/b.html | 1 |
  40. | | onerror | Script error. | | 0 |
  41. | Chrome | console | Uncaught SyntaxError: Unexpected token &lt; | b.com/b.html | 1 |
  42. | | onerror | Script error. | | 0 |
  43. | Safari | console | | | |
  44. | | onerror | Script error. | | 0 |
  45. | Opera | console | Syntax error at line 1 while loading: expected expression, got '<'<br /><!DOCTYPE html PUBLI <br />^ | a.com/a.html | 4 |
  46. | | onerror | Script error. | | 0 |
  47. 引入外部脚本该外部脚本会抛出异常
  48. a.com/a.html
  49. ```html
  50. <script type="text/javascript">
  51. window.onerror = function(m,f,l){
  52. var s = "message:"+m+", file:"+f+", line:"+l;
  53. console.log(s);
  54. window.status = s;
  55. };
  56. </script>
  57. <script type="text/javascript" src="b.com/b.js"></script>
  58. ```
  59. b.com/b.js
  60. ```javascript
  61. // some comments
  62. // for lines.
  63. var a = ;
  64. ```
  65. | Browser | type | Message | File | Line |
  66. |---------|---------|------------------------------------------------------------------------------------------------|--------------|------|
  67. | IE | console | 语法错误 | b.com/b.js | 3 |
  68. | | onerror | 语法错误 | b.com/b.js | 3 |
  69. | Firefox | console | syntax error | b.com/b.js | 3 |
  70. | | onerror | Script error. | | 0 |
  71. | Chrome | console | Uncaught SyntaxError: Unexpected token ; | b.com/b.js | 3 |
  72. | | onerror | Script error. | | 0 |
  73. | Safari | console | Script error. | | 0 |
  74. | | onerror | Script error. | | 0 |
  75. | Opera | console | Syntax error at line 3 while loading: expected expression, got ';'<br/>var a = ;<br/>--------^ | a.com/a.html | 4 |
  76. | | onerror | Script error. | | 0 |
  77. 特殊的如果是脚本中主动掷出的异常Safari 会稍有不同
  78. b.com/b.js
  79. ```javascript
  80. // some comments
  81. // for lines.
  82. throw new Error("test error on b.");
  83. ```
  84. | Browser | type | Message | File | Line |
  85. |---------|---------|---------------------------------------------|--------------|------|
  86. | IE | console | test error on b. | b.com/b.js | 3 |
  87. | | onerror | test error on b. | b.com/b.js | 3 |
  88. | Firefox | console | test error on b. | b.com/b.js | 3 |
  89. | | onerror | Script error. | | 0 |
  90. | Chrome | console | Uncaught Error: test error on b. | b.com/b.js | 3 |
  91. | | onerror | Script error. | | 0 |
  92. | Safari | console | Error: test error on b. | | |
  93. | | onerror | undefined | | 0 |
  94. | Opera | console | Uncaught exception: Error: test error on b. | a.com/a.html | 4 |
  95. | | onerror | Script error. | | 0 |
  96. ## 参考
  97. * [Cryptic Script Error. reported in Javascript in Chrome and Firefox](http://stackoverflow.com/questions/5913978/cryptic-script-error-reported-in-javascript-in-chrome-and-firefox)
  98. * [WebKit source that checks origin](http://trac.webkit.org/browser/branches/chromium/648/Source/WebCore/dom/ScriptExecutionContext.cpp?rev=77122#L301)
  99. * [Firefox source that checks](http://mxr.mozilla.org/mozilla-beta/source/dom/base/nsJSEnvironment.cpp#316)
  100. * [Same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy)