[英]clear javascript console in Google Chrome
I was wondering if I could clear up the console with some command..我想知道我是否可以用一些命令清理控制台..
console.log()
, can print... is there a command to clear up console?.. console.log()
,可以打印...有没有清理控制台的命令?...
I've tried to console.log(console);
我试过console.log(console);
and got this functions below...并在下面得到了这个功能......
assert: function assert() { [native code] }
constructor: function Console() { [native code] }
count: function count() { [native code] }
debug: function debug() { [native code] }
dir: function dir() { [native code] }
dirxml: function dirxml() { [native code] }
error: function error() { [native code] }
group: function group() { [native code] }
groupEnd: function groupEnd() { [native code] }
info: function info() { [native code] }
log: function log() { [native code] }
markTimeline: function markTimeline() { [native code] }
profile: function profile() { [native code] }
profileEnd: function profileEnd() { [native code] }
time: function time() { [native code] }
timeEnd: function timeEnd() { [native code] }
trace: function trace() { [native code] }
warn: function warn() { [native code] }
__proto__: Object
[ I guess there's no way to clear up the console... but I wanted someone to say it to me... ] [我想没有办法清理控制台......但我想有人对我说......]
Update:
console.clear()
is available in all browsers更新:console.clear()
在所有浏览器中可用
Update: As of November 6, 2012,
console.clear()
is now available in Chrome Canary.更新:截至 2012 年 11 月 6 日,console.clear()
现已在 Chrome Canary 中可用。
If you type clear()
into the console it clears it.如果您在控制台中键入clear()
它将清除它。
I don't think there is a way to programmatically do it, as it could be misused.我认为没有办法以编程方式做到这一点,因为它可能会被滥用。 (console is cleared by some web page, end user can't access error information) (控制台被某些网页清除,最终用户无法访问错误信息)
one possible workaround:一种可能的解决方法:
in the console type window.clear = clear
, then you'll be able to use clear in any script on your page.在控制台类型window.clear = clear
,您就可以在页面上的任何脚本中使用 clear 。
There's always the good ol' trick:总是有好的技巧:
console.log("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
or a shorter variation of the above:或更短的上述变化:
console.log('\n'.repeat('25'));
Not the most elegant solution, I know :) ... but works.不是最优雅的解决方案,我知道:) ...但有效。
For me, I usually just print a long "-----" separator line to help make the logs easier to read.对我来说,我通常只是打印一个长的“-----”分隔线来帮助使日志更易于阅读。
这似乎工作得很好:
console.clear();
If you use console.clear()
, that seems to work in chrome.如果您使用console.clear()
,这似乎适用于 chrome。 Note, it will output a "Console was cleared" message.请注意,它将输出“控制台已清除”消息。
Note, I got an error right after clearing the console, so it doesn't disable the console, only clears it.请注意,我在清除控制台后立即出错,因此它不会禁用控制台,只会清除它。 Also, I have only tried this in chrome, so I dont know how cross-browser it is.另外,我只在 chrome 中尝试过这个,所以我不知道它是如何跨浏览器的。
EDIT: I tested this in Chrome, IE, Firefox, and Opera.编辑:我在 Chrome、IE、Firefox 和 Opera 中对此进行了测试。 It works in Chrome, MSIE and Opera's default consoles, but not in Firefox's, however, it does work in Firebug.它适用于 Chrome、MSIE 和 Opera 的默认控制台,但不适用于 Firefox,但它确实适用于 Firebug。
If you want to just clear the console when debugging, you can simply click the "ban-circle" ⃠
button to clear console.log.如果只想在调试的时候清空控制台,可以直接点击“ban-circle” ⃠
按钮清空console.log。
Alternatively just press "Ctrl+L" to clear the console using your keyboard.或者,只需按“Ctrl+L”即可使用键盘清除控制台。
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()
.之后,您可以简单地使用console.API.clear()
。
you can use您可以使用
console.clear();
if you are working with javascript coding.如果您正在使用 javascript 编码。
else you can use CTR+L
to clear cosole editor.否则您可以使用CTR+L
清除 cosole 编辑器。
Press
CTRL+L
Shortcut to clear log
, even if you have ticked Preserve log
option. Press
CTRL+L
快捷方式清除log
,即使您已勾选Preserve log
选项。
Hope this helps.希望这可以帮助。
在 Mac 上,您也可以像在终端中一样使用⌘+K
。
console._inspectorCommandLineAPI.clear()
那是工作
A handy compilation of multiple answers for clearing the console programmatically (from a script, not the console itself):以编程方式清除控制台的多个答案的方便汇编(来自脚本,而不是控制台本身):
if(console._commandLineAPI && console._commandLineAPI.clear){
console._commandLineAPI.clear();//clear in some safari versions
}else if(console._inspectorCommandLineAPI && console._inspectorCommandLineAPI.clear){
console._inspectorCommandLineAPI.clear();//clear in some chrome versions
}else if(console.clear){
console.clear();//clear in other chrome versions (maybe more browsers?)
}else{
console.log(Array(100).join("\n"));//print 100 newlines if nothing else works
}
Instead of typing command just press:而不是键入命令只需按:
CLTRL + L
to clear chrome console清除 chrome 控制台
I use the following to alias cls
when debugging locally in Chrome (enter the following JavaScript into the console):在 Chrome 中本地调试时,我使用以下命令为cls
设置别名(在控制台中输入以下 JavaScript):
Object.defineProperty(window, 'cls', {
get: function () {
return console.clear();
}
});
now entering cls
in the console will clear the console.现在在控制台中输入cls
将清除控制台。
在 Chrome 控制台上用鼠标右键单击,我们可以选择清除控制台
Chrome - Press CTRL + L while focusing the console input. Chrome - 在聚焦控制台输入的同时按CTRL + L。
Firefox - clear()
in console input. Firefox - 控制台输入中的clear()
。
Internet Explorer - Press CTRL + L while focusing the console input. Internet Explorer - 在聚焦控制台输入的同时按CTRL + L。
Edge - Press CTRL + L while focusing the console input. Edge - 在聚焦控制台输入的同时按CTRL + L。
Have a good day!祝你有美好的一天!
Based on Cobbal's answer , here's what I did:根据 Cobbal 的回答,这就是我所做的:
In my JavaScript
I put the following:在我的JavaScript
我添加了以下内容:
setInterval(function() {
if(window.clear) {
window.clear();
console.log("this is highly repeated code");
}
}, 10);
The conditional code won't run until you ASSIGN window.clear (meaning your log is empty until you do so).在您分配 window.clear 之前,条件代码不会运行(这意味着您的日志在您这样做之前是空的)。 IN THE DEBUG CONSOLE TYPE:在调试控制台类型中:
window.clear = clear;
Mac OS 10.6.8 - Chrome 15.0.874.106 Mac OS 10.6.8 - Chrome 15.0.874.106
On MacOS :在MacOS 上:
On Linux :在Linux 上:
On Windows :在Windows 上:
To make it work in Firefox, userscripts
can be used.要使其在 Firefox 中工作,可以使用用户userscripts
。 Download GreaseMonkey
extension for FF.下载 FF 的GreaseMonkey
扩展。
document.addEventListener("keydown",function(event){
if(event.metaKey && event.which==75) //CMD+K
{
console.clear();
}
});
In the script, update the metadata with the value, //@include *://*/*
, to make it run on every pages.在脚本中,使用值//@include *://*/*
更新元数据,使其在每个页面上运行。 It will work only when the focus is on the page.只有当焦点在页面上时它才会起作用。 It's just a workaround.这只是一种解决方法。
I think this is no longer available due to 'security issues'.我认为由于“安全问题”,这不再可用。
console.log(console)
from code gives:代码中的console.log(console)
给出:
Console
memory: MemoryInfo
profiles: Array[0]
__proto__: Console
From outside of code, _commandLineAPI is available.从代码外部,_commandLineAPI 可用。 Kind of annoying because sometimes I want to just log and not see the old output.有点烦人,因为有时我只想记录而不是看到旧的输出。
I'm going to add to this, as it's a search that came out top on google..我要补充一下,因为这是一个在谷歌上名列前茅的搜索。
When using ExtJS / Javascript I insert this and the console is cleared out - unless there is an error..使用 ExtJS / Javascript 时,我插入它并清除控制台 - 除非有错误..
console.log('\033[2J');
I'm more than likely way off course, but this is how I clear the console for each page load/refresh.我很可能偏离了路线,但这就是我为每个页面加载/刷新清除控制台的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.