I use Firebug's console.log() for debugging my website. If I try viewing my website in browsers without Firebug then I get a console is not defined error. Is there a way to gracefully avoid this error?
I found this potential solution, but it seems a bit cumbersome. And ideas?
I always create my cross-browser wrappers for console.log alike functions and it looks like this:
It can be extended for any browsers. in IE when in debug I'd recommend putting this jquery code in last catch:
You must add JSON.serialize to Your script. IE doesn't have it (IE8 might have, I'm not sure)
Off the top of my head:
I don't think it gets much better than the workaround you link to. It's of course possible to melt it down to just defining
console.log()
and leave off rest, but in essence, you won't get around a construct like this.Only alternative that comes to mind is checking for console.log every time you call it, and that's even more cumbersome.
Honestly, I'd use that. It not only covers
console.log()
, but also every otherconsole
method, and in a decently short number of lines. The fact that it was first used in the Yahoo media player seems to suggest that it works excellently cross-browser, as well.That bit of code is your best bet, is actually decently elegant, and should work in most every case. As long as you comment above the snippet just what it is for (so as not to confuse future developers), you should be fine.
The solution of @OcuS is sure the best, but you can enhance it with mine: Check this way to log to FF Console: Log to Firefox Error Console from JavaScript
Then add to the firebugx.js this 3 lines inside IF:
So you will see the log of every console error, warn and debug even when the Firebug is closed
You can use this code to check if console object exists
Or this code
Also you can read this post http://alexandershapovalov.com/firebug-console-is-not-defined-60/ Inside the post link how to create jQuery wrapper for console