[removed] How do I print a message to the error console?

前端 未结 18 2107
闹比i
闹比i 2020-12-04 04:52

How can I print a message to the error console, preferably including a variable?

For example, something like:

print(\'x=%d\', x);
相关标签:
18条回答
  • 2020-12-04 05:29

    With es6 syntax you can use:

    console.log(`x = ${x}`);
    
    0 讨论(0)
  • 2020-12-04 05:31
    console.log("your message here");
    

    working for me.. i'm searching for this.. i used Firefox. here is my Script.

     $('document').ready(function() {
    console.log('all images are loaded');
    });
    

    works in Firefox and Chrome.

    0 讨论(0)
  • 2020-12-04 05:34

    A note about 'throw()' mentioned above. It seems that it stops execution of the page completely (I checked in IE8) , so it's not very useful for logging "on going processes" (like to track a certain variable...)

    My suggestion is perhaps to add a textarea element somewhere in your document and to change (or append to) its value (which would change its text) for logging information whenever needed...

    0 讨论(0)
  • 2020-12-04 05:37

    Exceptions are logged into the JavaScript console. You can use that if you want to keep Firebug disabled.

    function log(msg) {
        setTimeout(function() {
            throw new Error(msg);
        }, 0);
    }
    

    Usage:

    log('Hello World');
    log('another message');
    
    0 讨论(0)
  • 2020-12-04 05:38

    Install Firebug and then you can use console.log(...) and console.debug(...), etc. (see the documentation for more).

    0 讨论(0)
  • 2020-12-04 05:40

    One good way to do this that works cross-browser is outlined in Debugging JavaScript: Throw Away Your Alerts!.

    0 讨论(0)
提交回复
热议问题