Skip to content

Instantly share code, notes, and snippets.

@astorm
Created May 25, 2011 23:29
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save astorm/992233 to your computer and use it in GitHub Desktop.
Fix console.log in Chrome and Magento
<!-- Add to the package layout (via local.xml or however you prefer) -->
<!-- Also, best to remove before deployment, as some users report -->
<!-- it makes IE 7 crash -->
<default>
<reference name="content">
<block type="core/text" name="fix.console" as="fix.console">
<action method="setText">
<text><![CDATA[<script type="text/javascript">
iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.getElementsByTagName('body')[0].appendChild(iframe);
window.console = iframe.contentWindow.console;
console.firebug = "faketrue";
</script>]]></text>
</action>
</block>
</reference>
</default>
@brankoajzele
Copy link

@alan

In my Google Chrome "iframe.style = 'display:none';" did not work, iframe was visible. Changed it to "iframe.style.display = 'none';" and it worked.

P.S. Thanks for this great snippet. Really saved me :)

@astorm
Copy link
Author

astorm commented Dec 7, 2011

@ajzele

Ah, that looks more right, it's a pretty hacky fix so I'm not surprised it broke since I first wrote it. Thanks for the fix to my fix.

In case you're curious, the offending Javascript is in

// File js/varien/js.js
if (!("console" in window) || !("firebug" in console))
{
    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() {}
}

@guymoller
Copy link

in some version of IE8 I had the following error:

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64;
Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR
3.0.30729; Media Center PC 6.0)
Timestamp: Tue, 7 Feb 2012 11:29:49 UTC


Message: 'console' is null or not an object
Line: 183
Char: 25
Code: 0
URI: 


Message: HTML Parsing Error: Unable to modify the parent container element
before the child element is closed (KB927917)
Line: 0
Char: 0
Code: 0
URI: 

@astorm
Copy link
Author

astorm commented Feb 8, 2012

Adding an "if console" around the javascript in the text node should fix that. It sounds like you're using IE without any javascript debugger.

@JingleChannel
Copy link

thx, great snippet! but you need to comment it out after development. besides those errors in IE8 @guymoller mentioned, ie7 crashes completely. if (console) does not fix that.

@astorm
Copy link
Author

astorm commented Mar 8, 2012

Thanks for the heads up, but I'm not sure I follow. If you're wrapping everything in an if(console), how does the code run in IE 7?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment