alecswan alecswan - 1 month ago 6
jQuery Question

First failing jQuery.ready() breaks the rest

We allow users to write code which sometimes calls

jQuery.ready(function(){..})
multiple times. It seems like the first function call that throws an error prevents execution of the rest of the functions.

There was a discussion about this here with the solution wrapped
jQuery.ready()
in a delegate instead of wrapping the anonymous function parameter passed to
jQuery.ready(..)
.

How can I override
jQuery.ready(fn)
and wrap the passed-in function parameter in a delegate which wraps it in try/catch and the passes to
jQuery.ready(delegate)
?

Here is an example:

<head>
<script>
// here is some code from third-party developer that sometimes throws an error
jQuery.ready(function() {
if ((new Date()).getMonth() == 0)
throw Error("It's January!");
});
</script>
</head>

<body>
<script>
// here is my code which should run regardless of the error in the <head> script
jQuery.ready(function() {
alert("I need to run even in January!");
});
</script>
</body>


What can I do to make code in run regardless of errors in ?

Answer

If you need to catch your own errors, then catch them with your own try/catch:

$(document).ready(function() {
   try {
       // put your normal code here
   } catch (e) {
       // put any code you want to execute if there's an exception here
   }
});

This will allow all subsequent code to continue without pause. One might ask why you're getting errors thrown? And perhaps what you should be doing to fix that or handle that more directly?

If you want, you could do this and replace all the troublesome calls to jQuery.ready() with jQuery.safeReady():

jQuery.fn.safeReady = function(f) {
    jQuery.ready(function() {
        try {
            f();
        } catch(e) {
            // make it so you can see errors in the debugger 
            // so you would know if something was wrong
            if (window.console) {
                console.log(e);
            }
        }
    });
};
Comments