Home>

The previous version of jquery launched version 3.0, and the changes were very large.If i want to use 3.0, it is recommended to take a closer look at the new features,Otherwise it will cause some minor problems,For example, the following code.

Everyone rightTop ten new features in jquery 3.0Friends who do not understand can refer to the next article.

<html>
<head>
<meta charset="utf-8">
<script src="jquery-3.0.min.js"></script>
<script>
$(function () {
$("#container"). html ("Hello");
})
</script>
</head>
<body>
<div></div>
</body>
</html>

very simple,There are no problems at this time.Can display content normally,But if $(function () {...}) internal exception occurs,Here comes the disturbing problem

State after adding an exception

$(function () {
throw new error ("An error has occurred");
$("#container"). html ("Hello");
});

It is normal for the page not to display content.But the console doesn't show any error message,Obviously different from our expectations

Because when using jquery 2, in this case,The console can prompt errors

<script src="jquery-2.1.3.min.js"></script>
<script>
$(function () {
throw new error ("An error has occurred");
$("#container"). html ("Hello");
});
</script>

Solution

Because jquery 3.0 officially supports promises, you can use the promise form for .ready () and use fail to throw an exception

E.g

$.ready.then (function () {
throw new error ("An error has occurred");
$("#container"). html ("Hello");
}). fail (function (error) {
throw error;
});

The console can now display the error message

  • Previous Talking about Python object memory footprint
  • Next VS2015 running Cordova project exception solution after upgrading to Update2