alias to chrome console.log

I would like to know why the follow code doesn't work in the Google Chrome:

// creates a xss console log

var cl = ( typeof( console ) != 'undefined' ) ? console.log : alert;
cl('teste');

output: Uncaught TypeError: Illegal invocation

thanks.


https://groups.google.com/a/chromium.org/d/msg/chromium-bugs/gGVPJ1T-qA0/F8uSupbO2R8J

Apparently you can also defined log:

 log = console.log.bind(console);

and then the line numbers also work


When you write cl();, you're calling log in the global context.

Chrome's console.log doesn't want to be called on the window object.

Instead, you can write

cl = function() { return console.log.apply(console, arguments); };

This will call log in the context of console.


Unfortunately @SLaks answer isnt applied to IE because it uses window-object as context in console.log-method.

I would be suggest another way that doesnt depend on browser:

!window.console && (console = {});

console.debug = console.debug || $.noop;
console.info = console.info || $.noop;
console.warn = console.warn || $.noop;
console.log = console.log || $.noop;

var src = console, desc = {};
desc.prototype = src;
console = desc;

desc.log = function(message, exception) {
    var msg = message + (exception ? ' (exception: ' + exception + ')' : ''), callstack = exception && exception.stack;
    src.log(msg);
    callstack && (src.log(callstack));
    //logErrorUrl && $.post(logErrorUrl, { message: msg + (callstack || '') }); // Send clientside error message to serverside.
};