27

In PHP and other languages there are ways to suppress error/warning messages.

Is there a way in javascript or jquery to prevent errors and warnings from being written to the console log?

5
  • 1
    Yea, disable those messages in the console itself (There should be some filter options). However, why would you want to hide errors? That sounds dodgy... Commented Feb 4, 2014 at 9:59
  • 2
    How about console.clear(); ? Commented Feb 4, 2014 at 10:00
  • 20
    I hid all the errors and warnings from the console in my sites/apps by fixing the code. Commented Feb 4, 2014 at 10:02
  • I don't know if it was Darren's intention, but i went in a case where i needed to check if a resource was available and, if not, the code generated an error on the console. I didn't tested try { } catch(e) { } to see if it suppress the console error. I'll test it. Commented May 3, 2017 at 0:46
  • console.clear(); is good option to hide the console error because some time on running site we don't want to show error so we hide them in PHP. Commented Dec 4, 2019 at 10:18

8 Answers 8

29

A dirty way to hide all Javascript console warnings is by overriding the console object's warn method:

console.warn = () => {};

Obviously, it also works with other console methods

Sign up to request clarification or add additional context in comments.

1 Comment

this is what I was looking for, works in node.js as well
22

You can handle errors to some extent but if error is not handled by you then it goes to browser and you can not stop browser showing it. You can write code in such a way that you get minimum error and using try-catch where possible to handle the exceptions.

try
{
    //statements suspected to throw exception.
}
catch(e)
{
}

1 Comment

There must be a way to create a middleware (kindof) functionality that does error/exception handling for you. If someone finds (has found one) please reply.
3

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 () { }; // For confirmation again
        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.

1 Comment

Not really sure why you would need all that extra baggage if console.warn = () => {}; has the same effect. For instance console.warn and window['console']['warn'] refer to the same function, so they are overridden twice in this code.
2

Best to user console.clear(); in 90% of your cases it will be resolved

1 Comment

Clearing the console does not help in most situations - usually if you're wanting to hide certain types of console entries, it's because you are looking for something in the console of another type - probably console.log() output and the console is cluttered with error output.
2

try this

This code attaches an onerror event handler to the window object. When an unhandled JavaScript error occurs, this handler function will be called. Inside the function, you can perform actions such as logging the error, notifying the user, or any other necessary action. By returning true from this function, you prevent the default browser behavior of logging the error to the console, effectively suppressing the error message.

window.onerror = function(msg, url, line, col, error) {
    // Catch the error and do whatever is necessary
    return true; // Prevent the original error message from appearing in the console
};

3 Comments

I can't believe nobody suggested this before!
vote my answer..
Done, but the way you asked makes me think you didn’t write the answer. ChatGPT much?
1

This is browser based functionality, you may install any addon for browser then you can hide errors/ warnings

I went to about:config for Firefox and turned javascript.options.strict to false and the warnings went away. But I feel like this is not a solution.

howtocreate.co.uk/strictJSFirefox.html 

1 Comment

javascript.options.strict no longer exists.
0

you just simply add some chunk of code into the script it will works.! Chrome:

console._commandLineAPI.clear();

Safari:

console._inspectorCommandLineAPI.clear();

You can create your own variable, which works in both:

if (typeof console._commandLineAPI !== 'undefined') {
    console.API = console._commandLineAPI;
} else if (typeof console._inspectorCommandLineAPI !== 'undefined') {
    console.API = console._inspectorCommandLineAPI;
} else if (typeof console.clear !== 'undefined') {
    console.API = console;
}

After that, you can simply use

console.API.clear().

Comments

-3

I usually just go to the implementation of the function that includes that particular console.log warning and comment it out.

1 Comment

In your own code this is obviously the simplest solution. However, when we talk about 3rd party libraries, where this problem usually occurs, changing the library code will prevent you from updating the library. Also it doesn't work if you share dependencies with your team through a list of requirements (which is the de-facto standard nowadays).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.