Johan
Johan

Reputation: 35194

Reference function and "toggle" it

My current code:

window.onerror = function() {

    console.log('error', arguments);

    return false;
};

I would like to be able to bind and unbind the event whenever I want. Something like this:

var errorHandler = function() {

    console.log('error', arguments);

    return false;
};

var noop = function(){};

function toggleErrorHandler(enable){
    window.onerror = enable ? errorhandler : noop;
}

Is this the correct way to do it? My guess is that it's not :)

Upvotes: 0

Views: 50

Answers (1)

DhruvPathak
DhruvPathak

Reputation: 43235

It seems correct. A modification could be removing the use of noop function.

window.onerror = enable ? errorhandler : null ;

is enough to remove the handlers.

Upvotes: 3

Related Questions