Handling errors in jQuery(document).ready

前端 未结 5 665
庸人自扰
庸人自扰 2021-02-01 23:31

I\'m developing JS that is used in a web framework, and is frequently mixed in with other developers\' (often error-prone) jQuery code. Unfortunately errors in their jQuery(docu

相关标签:
5条回答
  • 2021-02-01 23:32

    Have you attempted wrapping the error-prone commands in try...catch brackets?

    $(function(){
        try {
            noObject.noMethod();
        } catch (error) {
            // handle error
        }
    });
    
    $(function(){
        try {
            alert("Hello World");
        } catch (error) {
            // handle error
        }
    });
    

    To avoid potential confusion, $(function(){ ... }); is functionally the same as $(document).ready(function(){ ... });

    0 讨论(0)
  • 2021-02-01 23:33

    You could re-reference jQuery before each of your code blocks. Your code would then use the fresh instance of the library, and it would execute. I've tested this in Safari 4.0.3 on OSX.

    <html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
        <script type="text/javascript">
            jQuery(document).ready(function() {
                nosuchobject.fakemethod();       //intentionally cause major error
            });
        </script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>    
        <script type="text/javascript">
            jQuery(document).ready(function() {
                alert("Hello!");                 //executed!
            });
        </script>   
    
    </head>
    <body>
    
    <p>hello world</p>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2021-02-01 23:52

    Here is solution, it will wrap any function sent to ready with try-catch block:

    (function($){
        $.fn.oldReady = $.fn.ready;
        $.fn.ready = function(fn){
            return $.fn.oldReady( function(){ try{ if(fn) fn.apply($,arguments); } catch(e){}} );
        }
    })(jQuery);
    
    0 讨论(0)
  • 2021-02-01 23:56

    I haven't tried this code, but it should work (at least, the idea should anyway). Make sure you include it AFTER jquery, but BEFORE any potentially buggy scripts. (Not necessary, see comments.)

    var oldReady = jQuery.ready;
    jQuery.ready = function(){
      try{
        return oldReady.apply(this, arguments);
      }catch(e){
        // handle e ....
      }
    };
    
    0 讨论(0)
  • 2021-02-01 23:58

    To answer your question, both of the ready blocks are essentially combined into one given the way jQuery works:

    <script type="text/javascript">
      jQuery(document).ready(function() {
          nosuchobject.fakemethod();       //intentionally cause major error
          alert("Hello!");                 //never executed
      });
    </script>
    

    So that's why its not alerting per the error above. I don't believe there is a way to fix this to make every ready function run regardless of former failures.

    0 讨论(0)
提交回复
热议问题