-1

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.

Ch Faizan Mustansar
  • 354
  • 3
  • 6
  • 18

3 Answers3

5

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.

Michał Dudak
  • 4,923
  • 2
  • 21
  • 28
1

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
    }
};
giammin
  • 18,620
  • 8
  • 71
  • 89
1

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

Fizer Khan
  • 88,237
  • 28
  • 143
  • 153