Search code examples
javascriptloggingdeveloper-tools

Check whether window.onerror is availale or not


In my javascript logging framework,I am catching unhandled exceptions through window.onerror handler.

How could I check for whether window.onerror is available or not.since in few browsers it would not recognize window.onerror.


Solution

  • You can check with :

    if(typeof window.onerror === 'undefined'){
    // you can't use window.onerror
    }else{
    //you can
    }
    

    Check out the MDN docs for a headstart on how you might override or create some kind of polyfill that would work cross browser:

    https://developer.mozilla.org/en/docs/DOM/window.onerror