Handling errors in jQuery(document).ready

前端 未结 5 788
庸人自扰
庸人自扰 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条回答
  •  Happy的楠姐
    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);
    

提交回复
热议问题