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?
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
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)
{
}
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.
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.Best to user console.clear(); in 90% of your cases it will be resolved
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
};
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
javascript.options.strict no longer exists.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().
I usually just go to the implementation of the function that includes that particular console.log warning and comment it out.
console.clear();?try { } catch(e) { }to see if it suppress the console error. I'll test it.