开发者

How do I see jQuery errors?

开发者 https://www.devze.com 2023-04-09 01:11 出处:网络
When an error occurs in jQuery code, IE9, Firefox and Chrome\'s script debugging tools dont see them - i开发者_如何学Pythons there a way to make them see jQuery errors?PROBLEM SOLVED

When an error occurs in jQuery code, IE9, Firefox and Chrome's script debugging tools dont see them - i开发者_如何学Pythons there a way to make them see jQuery errors?


PROBLEM SOLVED

The errors I was referring to were infact not errors at all e.g. a selector for a non-existent element does in fact not fail at all, it simply returns an array with 0 objects in it.


I don't know if you got this, but jQuery is plain javascript.

Every error that is not caught by the code will be thrown.

Wait... caught?

Yes, you can always avoid throwing errors if you want to manipulate their behaviour. For example, to log it internally or simply give a nicer message to the end user, this is done using, what we call, the try-catch block.

try {
   // you can do some pretty nasty stuff here

   addNumbers(12, 10);
}
catch(err) {
  // now that the error was thrown, you can "catch" it
  //  and play with it, but be nice.

  alert("Ops, there was an error!\n\n" + err);
}

You will never see any error on the console with this.

Back to jQuery, everything is javascript, so you will see every thrown error right in the console of your favorite browser.

0

精彩评论

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

关注公众号