Handling errors in jQuery(document).ready

前端 未结 5 670
庸人自扰
庸人自扰 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(){ ... });

提交回复
热议问题