Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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>
@ajzele

This comment has been minimized.

Show comment
Hide comment
@ajzele

ajzele Dec 7, 2011

@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 :)

ajzele commented Dec 7, 2011

@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

This comment has been minimized.

Show comment
Hide comment
@astorm

astorm 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() {}
}
Owner

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

This comment has been minimized.

Show comment
Hide comment
@guymoller

guymoller Feb 8, 2012

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: 

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

This comment has been minimized.

Show comment
Hide comment
@astorm

astorm 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.

Owner

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.

@kaisax

This comment has been minimized.

Show comment
Hide comment
@kaisax

kaisax Mar 8, 2012

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.

kaisax commented Mar 8, 2012

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

This comment has been minimized.

Show comment
Hide comment
@astorm

astorm 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?

Owner

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