Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for console.log statements in IE [duplicate]

Possible Duplicate:
'console' is undefined error for internet explorer

If you have console.log statements in your code, Internet Explorer will throw a JavaScript error (at least in IE7 which is what our intranet users have installed).

I am using Firefox for most of my development testing primarily because of the functionality provided by Firebug (where I use a lot of console statements) but I also need to test in IE.

if I add the following to my JavaScript, the error does not get thrown.

var debugging = false; if (typeof console == "undefined")      var console = { log: function() {} };   

The problem is that I would like to trigger an event if debugging mode is false. If I create a function to test whether debugging is false and do an action (at this point just an alert) but when I try do the following I receive an IE error saying Console is not defined.

var debugging = false; // or true    if (typeof console == "undefined")      var console = { log: function() {consoleMsg()} };     function consoleMsg() {     if(!debugging) {     alert('Console.log event in Production Code'); }    

If someone could help me to fix my code, provide a better way to help me achieve my goal, or direct me to a resource to edumacate myself I would be very appreciative.

like image 731
Michael BW Avatar asked Sep 28 '11 15:09

Michael BW


People also ask

What does console log () do?

The console. log() is a function in JavaScript that is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.

Why does console show undefined?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .


1 Answers

You don't have to jump through all these hoops. Simply check if the console exists before using it.

So, instead of:

console.log('foo'); 

Use:

window.console && console.log('foo'); 

...and you won't get any errors.


Alternatively, you could just check for it at the top of your script, and if it's undefined, just fill it with an empty function:

// At the top of your script: if ( ! window.console ) console = { log: function(){} }; // If you use other console methods, add them to the object literal above  // Then, anywhere in your script: console.log('This message will be logged, but will not cause an error in IE7'); 

For a more robust solution, use this piece of code (taken from twitter's source code):

// Avoid `console` errors in browsers that lack a console. (function() {     var method;     var noop = function () {};     var methods = [         'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',         'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',         'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',         'timeStamp', 'trace', 'warn'     ];     var length = methods.length;     var console = (window.console = window.console || {});      while (length--) {         method = methods[length];          // Only stub undefined methods.         if (!console[method]) {             console[method] = noop;         }     } }()); 
like image 192
Joseph Silber Avatar answered Sep 23 '22 10:09

Joseph Silber