clear javascript console in Google Chrome
我想知道是否可以使用某些命令清除控制台。
我试图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 |
[我想没有办法清理控制台...但是我希望有人对我说...]
Update: As of November 6, 2012,
console.clear() is now available in Chrome Canary.
如果在控制台中键入
我认为没有办法以编程方式进行此操作,因为它可能会被滥用。 (控制台已通过某些网页清除,最终用户无法访问错误信息)
一种可能的解决方法:
在控制台类型
总是有个好办法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | console.log(" "); |
我知道这不是最优雅的解决方案:) ...但是可以。
对于我来说,我通常只打印一条长长的" -----"分隔线以帮助使日志更易于阅读。
这似乎很好用:
1 | console.clear(); |
如果使用
注意,清除控制台后我立即收到错误消息,因此它不会禁用控制台,只会清除它。另外,我只在chrome中尝试过,所以我不知道它是如何跨浏览器的。
编辑:我在Chrome,IE,Firefox和Opera中对此进行了测试。它可以在Chrome,MSIE和Opera的默认控制台中使用,但不能在Firefox中使用,但是,它可以在Firebug中使用。
如果只想在调试时清除控制台,则只需单击"禁止圆圈"
或者,只需按" Ctrl + L"以使用键盘清除控制台。
铬:
1 | console._commandLineAPI.clear(); |
苹果浏览器:
1 | console._inspectorCommandLineAPI.clear(); |
您可以创建自己的变量,该变量在两种情况下均可使用:
1 2 3 4 5 6 7 | 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; } |
之后,您只需使用
希望这可以帮助。
您可以使用
1 | console.clear(); |
如果您使用的是JavaScript编码。
否则,您可以使用
在Mac上,您也可以像在Terminal中一样使用
1 | console._inspectorCommandLineAPI.clear() |
那在工作
方便的多个答案汇编,用于以编程方式(从脚本而不是控制台本身)清除控制台:
1 2 3 4 5 6 7 8 9 10 | 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(" "));//print 100 newlines if nothing else works } |
无需键入命令,只需按:
1 | CLTRL + L |
清除Chrome控制台
在Chrome控制台上,用鼠标右键单击,然后我们可以选择清除控制台
Chrome-集中控制台输入时,按CTRL + L。
Firefox-控制台输入中的
Internet Explorer-集中控制台输入时,按CTRL + L。
边缘-集中控制台输入时,按CTRL + L。
祝你有美好的一天!
根据Cobbal的回答,这是我所做的:
在我的
1 2 3 4 5 6 | setInterval(function() { if(window.clear) { window.clear(); console.log("this is highly repeated code"); } }, 10); |
在您指定ASSIGN window.clear之前,条件代码将不会运行(这意味着直到您这样做之前,您的日志为空)。在调试控制台类型中:
1 | window.clear = clear; |
Violà-清除自身的日志。
Mac OS 10.6.8-Chrome 15.0.874.106
在Chrome中进行本地调试时,我将以下内容用作
1 2 3 4 5 | Object.defineProperty(window, 'cls', { get: function () { return console.clear(); } }); |
现在在控制台中输入
在MacOS上:
在Linux上:
在Windows上:
要使其在Firefox中工作,可以使用
1 2 3 4 5 6 | document.addEventListener("keydown",function(event){ if(event.metaKey && event.which==75) //CMD+K { console.clear(); } }); |
在脚本中,使用值
我认为由于"安全问题",该功能不再可用。
代码中的
1 2 3 4 | Console memory: MemoryInfo profiles: Array[0] __proto__: Console |
从代码外部,可以使用_commandLineAPI。有点烦人,因为有时我只想登录而不看到旧的输出。
我将添加到此,因为它是在Google上排名最高的搜索。
当使用ExtJS / Javascript时,我将其插入并清除了控制台-除非出现错误。
1 | console.log('\033[2J'); |
我很可能会偏离路线,但这是我为每个页面加载/刷新清除控制台的方式。