Console Not Defined Javascript Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of javascript error console is undefined this site About Us Learn more about Stack Overflow the company Business Learn
Javascript Error Console Chrome
more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
Javascript Error Console Safari
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up
Javascript Error Console Firefox
js override console.log if not defined up vote 23 down vote favorite 8 Which solution do you recommend, the second is simpler ( less code ), but there are drawbacks on using it ? First: (Set a global debug flag) // the first line of code var debug = true; try { console.log } catch(e) { if(e) { debug=false; } }; // Then later javascript output console in the code if(debug) { console.log(something); } Second: override console.log try { console.log } catch(e) { if (e) { console.log = function() {} } }; // And all you need to do in the code is console.log(something); javascript console firebug share|improve this question edited Dec 15 '13 at 16:37 blunderboy 10.7k1158103 asked Sep 22 '10 at 9:30 Radu Maris 2,65622445 add a comment| 7 Answers 7 active oldest votes up vote 51 down vote accepted Neither, but a variation of the second. Lose the try...catch and check for existence of the console object properly: if (typeof console == "undefined") { window.console = { log: function () {} }; } console.log("whatever"); share|improve this answer edited Sep 22 '10 at 9:55 answered Sep 22 '10 at 9:35 Andy E 200k49354370 1 Presumably you either want to do this.console = ... or var console = ...? As you have it at the moment, you'd get an error in ECMAScript 5 strict mode. –Tim Down Sep 22 '10 at 9:48 1 @Tim: thanks, it was an oversight. I think window.console would be best for portability. –Andy E Sep 22
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about jquery error is not defined Stack Overflow the company Business Learn more about hiring developers or posting ads with firefox write to console us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is console verb a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Internet Explorer: “console is not defined” Error up vote 19 down vote favorite 4 http://stackoverflow.com/questions/3767924/js-override-console-log-if-not-defined I was using console.log() in some JavaScript I wrote and an error of: console is not defined was thrown in Internet Explorer (worked fine in other browsers). I have replaced it with: if (console) console.log("..."); If console is undefined, I would expect the condition to evaluate as false. Ergo, the statement console.log wouldn't be executed and shouldn't throw an error. Instead, an error of: console is not defined at character 4 http://stackoverflow.com/questions/9725111/internet-explorer-console-is-not-defined-error is thrown. Is this a IE bug? Or is that "if" condition really illegal? It seems absurd because if if (console) is illegal, then if (console==undefined) should be illegal too. How are you supposed to check for undefined variables? javascript internet-explorer internet-explorer-8 console undefined share|improve this question edited Nov 27 '13 at 11:48 Rohwedder 5043923 asked Mar 15 '12 at 17:36 matteo 89642141 13 console doesn't exist in IE unless the console is actually open. –Dagg Nabbit Mar 15 '12 at 17:38 BTW you may have some another console object, hence foolproof solution would be to check that console.log exists and is a function. –kirilloid Mar 15 '12 at 17:41 2 possible duplicate of 'console' is undefined error for internet explorer –Dagg Nabbit Mar 15 '12 at 17:41 2 Did I really write "Ergo"??? Or is that what @Matt McDonald edited? –matteo Apr 24 '12 at 21:41 Ergo was added by last editor "user1385191" :) –JOM May 14 '13 at 9:55 add a comment| 8 Answers 8 active oldest votes up vote 24 down vote accepted If console itself doesn't exist at all, it throws an error because you're accessing an undefined variable. Just like if(abc) {} throws an error. Since console
the code will break IE or any Firefox that doesn't have Firebug installed, since console and console.log will be undefined. so i wonder how about disabling it just by if https://www.sitepoint.com/community/t/how-not-to-let-console-log-to-cause-error-on-ie-or-other-browsers/4307 (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} }; at the very top of javascript code. It is true we could use if (debugging) console.log(oDiv) but then we need to constant turn debugging on and off on different browsers, and it is more troublesome to use "if (debugging)" every time. This following line will work too: if (typeof console == "undefined") var console javascript error = { log: function() {} }; except there might be some browser in which console is defined, but console.log is undefined... and using console.log() in those browser will cause an error. So the first solution in this post that test for both console and console.log is a better check. Paul_Wilkins 2008-10-05 19:57:10 UTC #2 That sounds like an interesting idea. I would have thought though that cross-browser testing would have javascript error console caught anything untoward that remained. I'm also not keen on the idea of having some console.log commands remain in production code that will get seen by visitors who also use firebug. winterheat 2008-10-05 20:02:31 UTC #3 pmw57 said: That sounds like an interesting idea. I would have thought though that cross-browser testing would have caught anything untoward that remained. I'm also not keen on the idea of having some console.log commands remain in production code that will get seen by visitors who also use firebug. yeah, when it is production, we can remove all the console.log() calls... or if we don't care about them remaining in the code, we can use var debugging = false; // or trueif (!debugging || typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} }; hm... but I think Firefox with Firebug doesn't let us redefine console... so maybe there is another way... (just found out that console.log can be reassigned on Firefox with Firebug) something like var debugging = false; // or trueif (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} };else if (!debugging && typeof console != "undefined") console.log = function() {}; you meant to use Browser sni