开发者

Error is not caught by jquery start() function

[Update. I need to be more precise, I see...] See the following example in javascript:

<html>
  <head>
    <script>
      window.onerror = function() {
        alert('error'); // this one works
        try {i.dont.exist += 0;}
        catch(e) {
          // do som开发者_高级运维e stacktrace stuff, this does not trigger
          alert(e.stack);
        }
      };
    </script>
    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js' type='text/javascript'></script>
    <script>
      $(document).ready(function() {
        foo[1]++;
      });
    </script>
  </head>
<body>
  <p>Hello world.</p>
</body>
</html>

The 2. alert is not triggered. Why? If I replace "foo[1]++" by "this is a bogus line" everything works and both alerts are triggered. Is there some run-time error problem?


The alert is not triggered because your error handler function was not successfully defined, due to your Javascript error :-) That block of code can't be parsed correctly so it isn't run.

Set it up this way:

<script>
  $(function() {
    window.onerror = function() {
      // ...
    };
  });
</script>

If it's in its own script tag, then it'll be OK. Now, you may want to reconsider delaying the definition of your error handler to the "ready" event handling, since you may have errors before that point is reached.

[edit] OK here is a complete example, and it works fine for me:

 <html>
   <head>
     <script>
       window.onerror = function() {
         alert("OH NO THERE HAS BEEN AN ERROR!");
       };
     </script>
     <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js' type='text/javascript'></script>
     <script>
       $(function() {
          here is some bogus stuff that will cause Javascript parse errors.
       });
     </script>
   </head>
   <body>
     <p>Hello world.</p>
   </body>
 </html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜