Do all browsers support this? I would like to output an error using console.log()
but was wondering if this supported by all browsers?
console.
I've done something like this in the past:
// Log to native console if possible, alert otherwise
window.console = typeof window.console === 'undefined'
? {log:function(/* polymorphic */){alert(arguments)}}
: window.console;
You can put that at the "top" of your JS and it works pretty nicely when you're forced to do debugging w/ a browser that doesn't support console
, and doesn't require you to change your other JS source that's already calling console.log
all over the place. Of course you might want to do something other than alert
to preserve your sanity...
http://jsfiddle.net/4dty5/
Here is a workaround for when console.log()
is not available. You have to retrieve the console.logs
yourself.
if (!window.console) window.console = {};
if (!window.console.log)
{
window.console.logs = [];
window.console.log = function (string)
{
window.console.logs.push(string);
};
}