Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding out if console is available

I was wondering, how can i find out with javascript if the console object is available?

i have the problem that if i forget to remove a debug output like console.log('sthg') i get errors in several browsers, if there is no firebug, or similar, active.

thanks for help

next to that problem i am interested in all informations about the console object. has anybody some documentation link, or so? is it a standard? and so on...

like image 401
helle Avatar asked Feb 09 '11 14:02

helle


2 Answers

Check the property exists as a member of window:

if (window.console) { } 

next to that problem i am interested in all informations about the console object. has anybody some documentation link, or so? is it a standard? and so on...

Check out the Firebug documentation for the Console API; Chrome and Safari implement most, but not all, of the methods listed there. There's no standard defining what should be in the console, so you'll need to test each browser to see if it supports the feature.

like image 161
Andy E Avatar answered Oct 04 '22 02:10

Andy E


A nice simple and short way of outputting to the console safely is as follows:

window.console && console.log('Debug message'); 
like image 23
Matthew O'Riordan Avatar answered Oct 04 '22 04:10

Matthew O'Riordan