Skip to content

Instantly share code, notes, and snippets.

@subzey
Forked from 140bytes/LICENSE.txt
Created December 12, 2011 10:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subzey/1466437 to your computer and use it in GitHub Desktop.
Save subzey/1466437 to your computer and use it in GitHub Desktop.
IE9 console.log patch

IE9 console.log patch

Very handy console.log method does exist in IE9, but for some obscure reason it's not a function. No, really

>> typeof console.log
   "object"

So it cannot be .call'ed or .apply'ed or be set as callback to String.prototype.replace. That's unfair.

This function wraps native console.log method into a function so it becomes a function again. It does nothing with normal browsers and older ones. It takes no arguments and returns nothing.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function(){ // No arguments
/* No more try-catch. Thanks, @atk! */
var // A big var declaration follows
a = this.console, // console object
b = a && a.log, // original console.log method (if console exists)
c = !b || b.call // If console.log not exists or is already call'able...
?
0 // ... fix is not needed. But we should provide any expression. Let it be a zero
:
// Otherwise, log is a new function now
a.log = function(){
// Get Function.prototype.apply method from the function itself
// and then call it using...
c.apply.call(
b, // old console.log from the closure as "this" for call method,
a, // console object as "this" for apply method,
arguments // passed arguments as-is for old console.log
);
}
// Ternary operator ends here. Assign new console.log (or 0) to c.
// Var declaration ends here
}
function(){var a=this.console,b=a&&a.log,c=!b||b.call?0:a.log=function(){c.apply.call(b,a,arguments)}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 subzey <subzey@gmail.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "ie9consolelog",
"description": "Patches console.log in IE9 so it becomes a real function",
"keywords": [
"ie9",
"console",
"patch"
],
"author": {
"name": "subzey",
"url": "https://github.com/subzey"
},
"contributors": [
{
"name": "Alex Kloss",
"url": "https://github.com/atk"
}
]
}
<!DOCTYPE html>
<title>Foo</title>
<pre id="log"></pre>
<script>
var forGreatJustice = function(){var a=this.console,b=a&&a.log,c=!b||b.call?0:a.log=function(){c.apply.call(b,a,arguments)}}
// The test
function testConsoleLog(){
if (!window.console || !console.log){
document.getElementById("log").innerHTML += "This browser doesn't support console.log<br />";
};
document.getElementById("log").innerHTML += "Trying call console.log.apply...<br />";
try {
console.log.apply(console, [1,2,3]);
document.getElementById("log").innerHTML += "Works okay<br />";
} catch (e) {
document.getElementById("log").innerHTML += "Fail: " + e.name + " " + e.message + "<br />";
}
};
if (window.console && console.log){
var previousConsoleLog = console.log;
};
testConsoleLog();
forGreatJustice();
if (window.console && console.log){
if(previousConsoleLog == console.log){
document.getElementById("log").innerHTML += "Console.log was *NOT* patched<br />";
} else {
document.getElementById("log").innerHTML += "Console.log was patched<br />";
};
};
testConsoleLog();
</script>
@atk
Copy link

atk commented Dec 13, 2011

You can lose the if statement as well as the last ";":

function(a,b){try{a=console;b=a.log;b.call||(a.log=function(){Function().apply.call(b,a,arguments)})}catch(e){}}

@subzey
Copy link
Author

subzey commented Dec 13, 2011

@atk, thanks for your suggestion!
I suppose we can forget about old FF2 (3?) with its "empty catch statement" parser bug.

One tricky thing I've noticed is ?: operator allows AssignmentExpression within and a?0:b=42 is 1 byte shorter than a||(b=42)

So i came up with this code:
function(){try{var a=console,b=a.log,c=b.call?0:a.log=function(){c.apply.call(b,a,arguments)}}catch(e){}}
It should work, but I cannot test it in IE9 at the moment

@atk
Copy link

atk commented Dec 14, 2011

You're welcome. Maybe we could lose the try/catch?

function(){var a=this.console,b=a&&a.log,c=!b||b.call?0:a.log=function(){c.apply.call(b,a,arguments)}}

Tested in IE9 (haven't got older IEs handy at the moment).

@subzey
Copy link
Author

subzey commented Dec 14, 2011

@atk, looks nice. I'll update the code

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