Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 866
alecswan Avatar asked Nov 22 '11 00:11

alecswan


People also ask

What does the jQuery ready () function do?

ready() method offers a way to run JavaScript code as soon as the page's Document Object Model (DOM) becomes safe to manipulate. This will often be a good time to perform tasks that are needed before the user views or interacts with the page, for example to add event handlers and initialize plugins.

What is $( document ready () method in jQuery?

jQuery ready() Method The ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.

What is difference between $( document .ready function () vs $( function ()?

The key difference between $(document). ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.

Is $( document .ready necessary?

No, it is not necessary. You can either put the script tag right before the body closing tag or if you are supporting IE9+ you can use native JS.


2 Answers

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);
            }
        }
    });
};
like image 138
jfriend00 Avatar answered Sep 28 '22 09:09

jfriend00


You can overwrite it like that:

jQuery.fn.ready = function(param){
    // do whatever you wish
};

but you should not do it. See this jsfiddle for a reason.

The example from my jsfiddle shows, that the above solution would also overwrite commonly used jQuery feature and can severely break your code.

Just rewrite your code - either by changing .ready() into .delegate() or by enclosing poorly written code within try {...} catch(...) {...} statements.

like image 43
Tadeck Avatar answered Sep 28 '22 08:09

Tadeck