How to clear the javascript console programmatically?

爷,独闯天下 提交于 2019-12-23 08:53:38

问题


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?


回答1:


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




回答2:


In Firefox, as of July 25, 2019, I first tried typing:

console.API.clear();

But, that gave a message in the console that: console.API is undefined. So, smelling that something was probably right with the answer given above, but not exactly, I then typed the following in the console:

console.clear();

That worked, and the console was cleared and gave a message that the console had been cleared. I do not know if this would work in any other browser besides Firefox, and, of course, I only know that it worked today.



来源:https://stackoverflow.com/questions/31261667/how-to-clear-the-javascript-console-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!