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...
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.
A nice simple and short way of outputting to the console safely is as follows:
window.console && console.log('Debug message');
here's what i use. bear in mind that i am only half-heartedly supporting browsers with no support for console. and i only ever use console.log(), but you can see how it can be extended to support console.dir(), console.info(), etc
var console = console || {
"log": function(stuff) {}
};
I like it because calling it does not cause an error, but it returns [undefined], which i think is appropriate.
Note that many many people before (and after) us have written similar polyfills:
https://gist.github.com/search?q=console+%7C%7C+console
Defined by firebug, IE8 (need to open the developer tools with F12), Chrome, etc but there is no defined spec for it. There is a console.log wrapper that makes it a very easy to use, cross browser logging solution so if the console doesn't exist your code doesn't explode.
try{
console.log("test")
}
catch(e){
console={},
console.log=function(a){}
}
just put it at the top of your JS file and then use console.log(); without any worry for browser error, i also had this error in IE9