I'm using Firebug and have some statements like:
console.log("...");
in my page. In IE8 (probably earlier versions too) I get script errors saying 'console' is undefined. I tried putting this at the top of my page:
<script type="text/javascript">
if (!console) console = {log: function() {}};
</script>
still I get the errors. Any way to get rid of the errors?
After having oh so many problems with this thing (it's hard to debug the error since if you open the developer console the error no longer happens!) I decided to make an overkill code to never have to bother with this ever again:
Personaly I only ever use console.log and console.error, but this code handles all the other functions as shown in the Mozzila Developer Network: https://developer.mozilla.org/en-US/docs/Web/API/console. Just put that code on the top of your page and you are done forever with this.
I'm using fauxconsole; I modified the css a bit so that it looks nicer but works very well.
In my scripts, I either use the shorthand:
or, if it's not possible or feasible to edit every console.log line, I create a fake console:
Based on two previous answers by
and the documentations for
Here's a best effort implementation for the issue, meaning if there's a console.log which actually exists, it fills in the gaps for non-existing methods via console.log.
For example for IE6/7 you can replace logging with alert (stupid but works) and then include the below monster (I called it console.js): [Feel free to remove comments as you see fit, I left them in for reference, a minimizer can tackle them]:
and console.js:
You can use console.log(...) directly in Firefox but not in IEs. In IEs you have to use window.console.
You can use
console.log()
if you haveDeveloper Tools
in IE8 opened and also you can use theConsole
textbox on script tab.