Ignore javascript syntax errors in a page and continue executing the script

前端 未结 11 851
日久生厌
日久生厌 2020-12-02 18:21

I develop plugins for WordPress. It uses some jquery in the user side (themes) as a jquery plugin. The problem is, when there is an javascript error with other plugins made

相关标签:
11条回答
  • 2020-12-02 18:34

    You should be able to swallow any error using the error event:

    $(window).error(function(e){
        e.preventDefault();
    });
    

    I've never attempted this, but it should work in theory.

    It should be noted that this probably isn't a great solution to your problem. It could be that your plugin is interfering with other plugins. It is, of course, possible that the errors are no fault of your own, but generally speaking (with publicly released plugins) not the case.

    0 讨论(0)
  • 2020-12-02 18:35
    <script>
    var _z = console;
    Object.defineProperty(window, 'console', {
        get: function(){
            if (_z._commandLineAPI) {
             return true;   
            }
            return _z;
        },
        set: function(val) {
            _z = val;
        }
    });
    </script>
    
    0 讨论(0)
  • 2020-12-02 18:37

    If you page is running. Use:

    $(window).error(function(e){
            e.preventDefault();
    });  // IGNORE ALL ERROR JQUERY!
    

    or use:

    window.onerror = function(){
       return true;
    }  // IGNORE ALL ERROR JAVASCRIPT!
    
    0 讨论(0)
  • 2020-12-02 18:38

    A regular try catch statements won't work for these types of errors.

    Possible workarounds:

    Use inline-css to float the bar to the left instead of jQuery (I am not sure what the full function of your plugin is but if it just floats the bar to the left why not just use css?)

    Have a invisible iframe which tries to run some code on the parent page. if it fails than alert the user(from within this iframe) that it wasn't your fault :)

    0 讨论(0)
  • 2020-12-02 18:43

    This is worked for me:

    try{
        //your code
    }
    catch(error)
    {
        return true;
    }
    
    0 讨论(0)
  • 2020-12-02 18:46

    Perhaps you could try putting your js in an html object, so that it's executed in a separate page. That probably won't help much if the js on the main page wont run to interact with the object. Just something to play with.

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