Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the javascript console programmatically?

How we can clear the Console in Chrome, Firefox and other browsers. I've tried the following commands, but none is working:

Chrome: clear()

Firefox: console.clear()

Any ideas?

like image 371
Mediasoft Avatar asked Jul 07 '15 06:07

Mediasoft


People also ask

How do I clear the console in JavaScript?

Use the short cut Ctrl + L to clear the console. Use the clear log button on the top left corner of the chrome dev tools console to clear the console. On MacOS you can use Command + K button.

Which code is used to clear the Consol?

clear() The console. clear() method clears the console if the console allows it.

How do you clear the console on the react app?

CTRL + L as Shortcut to clear Developer Console.


1 Answers

For every browser it is different so you can write some script so that it will work for different browsers. or you can use this script

console.API;

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

console.API.clear();

so on for other browsers too.

Note: Successfully tested (after edit, 08/2016) in Safari v9.1 for Mac OS, and Chrome v52.0 for Mac OS

like image 53
Govind Mantri Avatar answered Sep 21 '22 08:09

Govind Mantri