Welcome to %s forums

<No. 1 Community for developers, by developers/>

Login Register

How to check Compatibility of console in javascript?

Get support on JavaScript,jQuery, AJAX and other related technology.
Post Reply
admin
Site Admin
Posts: 45

How to check Compatibility of console in javascript?

Post by admin » Sun Oct 20, 2019 5:08 pm

When using or emulating Internet Explorer 8 or earlier versions (e.g. through Compatibility View / Enterprise Mode) the console will only be defined when the Developer Tools are active, so console.log() statements can cause an exception and prevent code from executing. To mitigate this, you can check to see if the console is available before you log:

Code: Select all

if (typeof window.console !== 'undefined')
{
console.log("Hello World");
}
Or at the start of your script you can identify if the console is available and if not, define a null function to catch all of your references and prevent exceptions.

Code: Select all

if (!window.console)
{
console = {log: function() {}};
}
Note this second example will stop all console logs even if the developer window has been opened.
Using this second example will preclude use of other functions such as console.dir(obj) unless that is specifically added.

A browser's debugging console or web console is generally used by developers to identify errors, understand flow of execution, log data and for many other purpose at runtime. This information is accessed through the console object.

Post Reply