Skip to content Skip to sidebar Skip to footer

Restoring Console.log()

For some reason, the prototype framework (or another JavaScript code) that is shipped with Magento is replacing standard console functions, so I can't debug anything. Writing down

Solution 1:

Since original console is in window.console object, try restoring window.console from iframe:

var i = document.createElement('iframe');
i.style.display = 'none';
document.body.appendChild(i);
window.console = i.contentWindow.console;
// with Chrome 60+ don't remove the child node// i.parentNode.removeChild(i);

Works for me on Chrome 14.

Solution 2:

For example,

deleteconsole.log

would also restore console.log:

console.log = null;
console.log;         // nulldeleteconsole.log;
console.log;         // function log() { [native code] }

Solution 3:

Magento has the following code in /js/varien/js.js - comment it out & it will work.

if (!("console"inwindow) || !("firebug"inconsole))
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

Solution 4:

Just in case that someone face this same situation. I did not replied to the original answer for Xaerxess because I don't have enough reputation to do it. Looks like that is the correct answer, but for some reason I notice sometimes it works in my software and sometimes not...

So I tried completing deleting before running the script and looks like everything is working fine 100% of times.

if (!("console"inwindow) || !("firebug"inconsole))
{

  console.log = null;
  console.log;         // nulldeleteconsole.log;

  // Original by Xaerxessvar i = document.createElement('iframe');
  i.style.display = 'none';
  document.body.appendChild(i);
  window.console = i.contentWindow.console;

}

Thank you to everybody.

Solution 5:

delete window.console restores the original console object in Firefox and Chrome.

How does this work? window is a hosted object and usually it is implemented with a common prototype between all instances (you have many tabs in the browser).

Some dumb developers of external libraries/frameworks (or Firebug, etc.) override property console of the window instance, but it doesn't corrupt window.prototype. By the delete operator we are back dispatching from the console.* methods to prototype code.

Post a Comment for "Restoring Console.log()"