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

前端 未结 18 2193
闹比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:28

    As always, Internet Explorer is the big elephant in rollerskates that stops you just simply using console.log().

    jQuery's log can be adapted quite easily, but is a pain having to add it everywhere. One solution if you're using jQuery is to put it into your jQuery file at the end, minified first:

    function log()
    {
        if (arguments.length > 0)
        {
            // Join for graceful degregation
            var args = (arguments.length > 1) ? Array.prototype.join.call(arguments, " ") : arguments[0];
    
            // This is the standard; Firebug and newer WebKit browsers support this.
            try {
                console.log(args);
                return true;
            } catch(e) {
                // Newer Opera browsers support posting erros to their consoles.
                try {
                    opera.postError(args);
                    return true;
                } 
                catch(e) 
                {
                }
            }
    
            // Catch all; a good old alert box.
            alert(args);
            return false;
        }
    }
    

提交回复
热议问题