Wednesday, December 18, 2013

Javascript - console undefined

Console is not implemented in old browsers, e.g. IE 8 or below. So if your code contains console.xxx() , it gives error and terminates in your script.

Therefore, to give support of cross-browsers to your webpage, you can include a global javascript file which will probably being included in most of the html files. And put the following code to it.


/** for browsers does not have console to avoid error -- strat **/
if (!window.console && !console){
 var console = new MyConsole();
}

function MyConsole(){
 this.log = function log(str){}
}
/** for browsers does not have console -- end **/

Console in different browsers might have different functions, here is some general functions. You might add all to the MyConsole class.
"log", "info", "warn", "error", "debug", "trace"

There are several solutions to support console in the internet, you might check them out also.

No comments:

Post a Comment