Question

Can anyone plase let me know that how can I use console.log in my application throgh modernizr.js which needs to be openned in almost every browser.

When I tried to run it in IE 8, it is giving me an error that console is undefined.

Was it helpful?

Solution

You may create a simple console polyfill for IE:

window.console = window.console || {
  log: function () {}
};

This won't have any effect on modern browsers. It'll just prevent undefined console error in IE.

OTHER TIPS

You can include console polyfill in your page https://github.com/paulmillr/console-polyfill which add empty function if console.log is not available(ex: IE6)

You can also try console.log wrapper https://github.com/patik/console.log-wrapper

Console.log object is a features of some browser (Firefox, Chrome,...)

In IE console.log is only available after you have opened the Developer Tools F12

to avoid those errors you have to check for existence:

if ( window.console && window.console.log ) {
  // console is available
}

or you can create a console fallback to avoid checking every time writing the code below on top of you .js file or in your case before referencing modernzer.js:

window.console = window.console || { 
    log: function (msg) {
        alert(msg); //if you don't want alerts instead of logs comment this line
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top