문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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
    }
};
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top