Which browsers support console.log()?

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.log("Error etc");

Answers:

Answer

No, not all browsers support console.log as it is not part of the standard and is an extension of the DOM and thus you should not count on its presence. To make your code resilient you should assume it does not exist and code accordingly.

Answer

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/

Answer

It is now August 2015, and I think the current answer to this question is:

All major browsers on mobile and desktop support console.log. (caniuse)

It is not part of any standard, but it is now an expected deliverable of a complete modern browser.

However:

If you need to support old browsers (IE<10), more mobile browsers, or users running experimental browsers, then it might be a good idea to use a polyfill (1,2,3,4) to ensure window.console exists.

It might not work in WebWorkers on all browsers. (MDN)

In UC Browser and Opera Mini the functions will run, but you won't see the output. (caniuse)

But for the vast majority of web users now, we can assume console.log will work as expected.

Answer

Make a wrapper function:

function log(text) {
  if (window.console) {
     window.console.log(text);
  }
}
Answer

Most browsers do, however Internet Explorer 9 has issues with it where it wont run any javascript unless you have the debug window open. Took us hours to find the solution to that problem.

Answer

This code will check to see if the function exists and create a dummy in case it does not. Credit: StackOverflow

if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function () { };
Answer

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);
    };
  }
Answer

Although not all browsers support that, it can be accomplished with a small chunk of code.

In his book, "Secrets of Javascript Ninja", John Resig (creator of jQuery) has a really simple code which will handle cross-browser console.logissues. He explains that he would like to have a log message which works with all browsers and here is how he coded it:

 function log() {
  try {
     console.log.apply(console, arguments);
  } catch(e) {
  try {
     opera.postError.apply(opera, arguments);
  }
  catch(e) {
     alert(Array.prototype.join.call( arguments, " "));
  }
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.