Sunday, April 8, 2012

How to clear the javascript console in google chrome or firebug programmatically

Just a quick tip.

If  you are extensively working with the console in the chrome developer tools and the console in Firefox/firebug and you find the need to clear the console many times while you are testing your code, both Chrome and Firefox have a feature that lets you do that programmatically.



I feel that the Firefox version of the command makes more sense. I have absolutely no idea why the Chrome guys chose to do it differently.

If you are a mouse person, in chrome, you can use the context menu and select Clear Console. If you are using firefox instead, you can use the clear button on top of the firebug console. But I guess you know that already.

There are a few gocha's though.

  • While you can invoke console.clear() in Firefox from within your own JavaScript to clear the console, you cannot use the clear() function in chrome to clear the console from your own JavaScript.
  • Another point to be noted is that in case you are getting an "undefined" message upon typing the clear() function in chrome, you need to disable the option to preserve logs upon navigation for the console. You can do that by right clicking on the console and unticking the "preserve log upon navigation".
If you are a fan of typing, chrome also lets you clear the console via a longer api command - console._inspectorCommandLineAPI.clear(). IMO, the shorter version should be sufficient if you want to save typing time.

Happy Programming :) 
Signing Off 

Ryan Sukale

No comments: