Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is console.log an empty function on some sites in Chrome?

Go to Twitter's login page and type the following in the console:

window.addEventListener('keypress', function(e){console.log('hello')}, true)

(NOTE: how the third parameter is set to true which enables event capture. This causes events to be intercepted first by the window before being consumed by a child element.)

Try pressing some keys. Notice how hello isn't output to the console. Adding an event listener for keydown or keyup doesn't change anything.

hello will get output on most websites, but not on sites like Twitter or Gmail.

Why? What's stopping the event listener?

EDIT: Seems to work as expected on Firefox. But not Chrome. Why isn't Chrome firing the event listener as expected?

EDIT 2: As deduced by a few people below, console.log is an empty function on Chrome for sites like Twitter and Gmail. Why is that?

like image 751
Matm Avatar asked Sep 26 '12 23:09

Matm


People also ask

Why my console is not working in chrome?

To restore the console in new tab go to console and type delete window. console it will return true after that restart the chrome and you are good to go.

How do I get rid of the function log in chrome?

Use the short cut Ctrl + L to clear the console. Use the clear log button on the top left corner of the chrome dev tools console to clear the console. On MacOS you can use Command + K button.

Why does chrome console say undefined?

The reason for that is that the console prints the value that is returned by the last statement. console. log(...) returns undefined (well, returns nothing, void, for that matter), so you get undefined. If the last statement returns something else (or has a value), that would be displayed.

Where does console log go in chrome?

Console Logs in Chrome: In Google Chrome, the Console Logs are available as a part of Chrome Dev Tools. To open the dedicated Console panel, either: Press Ctrl + Shift + J (Windows / Linux) or Cmd + Opt + J (Mac).


Video Answer


3 Answers

Because those sites have specifically set (for webkit apparently):

console.log = function(){};

However, in Chrome you can restore the original log() functionality by issuing this command in console:

console.log = console.__proto__.log

Then you can do this:

window.addEventListener('keypress', function(e){console.log('hello')}, true)

And it should work as expected.

like image 187
canon Avatar answered Oct 21 '22 07:10

canon


Get it from an iframe:

function setConsole() {
  var iframe = document.createElement('iframe');
  iframe.style.display = 'none';
  document.body.appendChild(iframe);
  console = iframe.contentWindow.console;
  window.console = console;
}

(and then call it)

setConsole()

source: https://gist.github.com/cowlicks/a3bc662b38c36483b35f74b2b54e37c0

like image 23
palanga Avatar answered Oct 21 '22 06:10

palanga


Developers like to put console.log() statements in their code for troubleshooting/debugging. Sometimes, they forget to take them all out when checking in production code. A simple safeguard against this is to redefine console.log() in production code to be an empty function that does nothing so even if a developer accidentally leaves one in, it won't cause any output or throw an exception when there is no console object when not running the debugger (like in IE).

Some browsers may protect against the replacing of that function by making it read-only such that it can't be overwritten in this way, but if they do that, then the console object must exist so there's still no harm from an occasional console.log() statement. The main harm of these leftover console.log() statements is in IE where it causes an exception to be thrown because the console object doesn't exist unless the debugger is being run.

like image 42
jfriend00 Avatar answered Oct 21 '22 07:10

jfriend00