[caption id="attachment_267" align="alignleft"]
'console' is not declared (IE8)[/caption]
Almost all modern browsers have a validation function for debugging, so you can see included external files and adapted styles, and you can check the operation of even HTML dynamically changed by JavaScript, jQuery, etc. in real time, making site creation much easier.
Especially in JavaScript, with the addition of the console object, it is very convenient to be able to verify all the debugging output on the browser console side of the back by aggregating all the debugging outputs without stopping the front-end processing, where the operation was checked while stopping the processing with the alert() method.
I think that people who have used the alert() method in JavaScript development will sympathize with this console object, but it is quite easy to use. Everything is processed behind the scenes, so the output does not appear on the surface, and the flow of the program does not stop, and depending on the browser implementation, it is easy to list the contents of arrays and objects. It has become a mandatory object for JavaScript coding in my personal life.
However, there is also a "pitfall" where it is too easy to use, and the site is published while leaving the debug code in JavaScript as it is... There are many things like that (... Just me? )。
And if there is only one console object left for debugging, the error will occur in older browsers that don't have a console object (obviously...).
In particular, IE8 browsers are troublesome, and they fall into "pitfalls" when verifying operation in IE9 with a console object.
This is because the developer mode added in IE9 (which starts with [F12]) is useful, and it also has the emulation function of IE7 and IE8, but it will pass through errors for console objects that are not originally implemented in IE8. This is probably because the original browser, IE9, supports console objects and does not internally switch around them during emulation, but this is because even if the console object for debugging remains in JavaScript, it will be processed normally, so "JavaScript that works normally in IE8" There may be cases where it is published as it is (in fact, I did).
──So, after the release, pure IE8 users report the error.
But my IE9 emulation function doesn't bugg... "Huh? Why?" So it will be.
If you tell me the content of the pure IE8 error ("The 'console' object is not defined"), you will notice it immediately, but users rarely check the details of the error, so you will receive fragmentary reports such as "it doesn't work here" or "it gets an error here". In fact, due to the description of the console object, "some functions are not working" described below, but in this case, it is difficult to identify the cause and it is quite confusing.
Well, don't leave the debugging description in the source code that releases it, not just the console object! … That's the theory, but I think there are times when you think about cutting back later, or debugging again when a function is added, and you dare to leave it.
In such cases, it is safe to implement it in browsers that do not support console objects without errors.
In addition, we will make it our own function so as not to kill the usability of the console object so that it can be called at any time.
[code lang="js"] function debug(string, enable_flag){ if(enable_flag){ if(typeof console != 'undefined'){ console.info(string); } else{ alert(string); } } return true; } [/code] and where you want to call,
[code lang="js"] debug("characters you want to display, etc.", true); [/code] will execute the console.info() method for browsers with a valid console object, and the alert() method for invalid browsers to display a dialog. If you want to temporarily disable the debugging function, you can specify "false" in the second argument and nothing will be done.