Skip to content

Instantly share code, notes, and snippets.

@seajones
Created August 18, 2013 13:35
Show Gist options
  • Select an option

  • Save seajones/6261702 to your computer and use it in GitHub Desktop.

Select an option

Save seajones/6261702 to your computer and use it in GitHub Desktop.
IE-Safe console.log
/*
Some versions of IE have a bug that stops applications working if they use console.log and the developer window is closed.
This is obviously a fairly big issue, especially because developers will often have the dev window open, meaning that they
struggle to recreate the problems their users report.
The answer is not to check for IE, but to check that console.log actually exists before invoking it. You can do this with
if conditionals, but this isn't the most elegant solution. Here's mine.
*/
console&&console.log&&console.log('stuff to log');
// Granted, it's a bit long, so you can always use:
console = console || {};
"function"!=typeof console.log&&(console.log=function(){return null});
// ...thus rendering console as an object if it doesn't exist (relieving us of any warning level errors caused by using it) and
// rendering console.log as a function that will do nothing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment