How do I print debug messages in the Google Chrome JavaScript Console?

前端 未结 14 913
清酒与你
清酒与你 2020-11-27 08:54

How do I print debug messages in the Google Chrome JavaScript Console?

Please note that the JavaScript Console is not the same as the JavaScript Debugger; they have

14条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 09:45

    Improving further on ideas of Delan and Andru (which is why this answer is an edited version); console.log is likely to exist whilst the other functions may not, so have the default map to the same function as console.log....

    You can write a script which creates console functions if they don't exist:

    if (!window.console) console = {};
    console.log = console.log || function(){};
    console.warn = console.warn || console.log;  // defaults to log
    console.error = console.error || console.log; // defaults to log
    console.info = console.info || console.log; // defaults to log
    

    Then, use any of the following:

    console.log(...);
    console.error(...);
    console.info(...);
    console.warn(...);
    

    These functions will log different types of items (which can be filtered based on log, info, error or warn) and will not cause errors when console is not available. These functions will work in Firebug and Chrome consoles.

提交回复
热议问题