How to quickly and conveniently disable all console.log statements in my code?

后端 未结 28 2154
深忆病人
深忆病人 2020-11-22 16:48

Is there any way to turn off all console.log statements in my JavaScript code, for testing purposes?

28条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 17:34

    After doing some research and development for this problem, I came across this solution which will hide warnings/Errors/Logs as per your choice.

        (function () {
        var origOpen = XMLHttpRequest.prototype.open;
        XMLHttpRequest.prototype.open = function () {        
            console.warn = function () { };
            window['console']['warn'] = function () { };
            this.addEventListener('load', function () {                        
                console.warn('Something bad happened.');
                window['console']['warn'] = function () { };
            });        
        };
    })();
    

    Add this code before JQuery plugin (e.g /../jquery.min.js) even as this is JavaScript code that does not require JQuery. Because some warnings are in JQuery itself.

    Thanks!!

提交回复
热议问题