At the time of writing, IE (versions 7 and below) does not have a JavaScript console supporting console.log.
(Request to the IE8 team: Your product is still in beta. You must have a logging call somewhere. Publish it, please. Please.)
There are quite a few good console.log() equivalents out there, not the least of which are Faux Console and the Yahoo User Interface Logger Widget. For extremely light-weight applications, though, there was nothing that did just what I wanted, so I wrote one.
The JavaScript code:
// rudimentary javascript logging to emulate console.log(). If there
// already exists an object named "console" (defined by most *useful*
// browsers :p) then we won't do anything here at all.
if (typeof (console) === 'undefined') {
// define "console" namespace
console = new function() {
// this is the Id of the console div. It doesn't actually need
// to be a div, as long as it has an innerHTML property.
this.ConsoleDivId = "JavaScriptConsole";
// maintains a reference to the console output div, so that we
// don't have to call document.getElementById a bunch of times.
this.ConsoleDiv = null;
// allows us to cache whether or not the console div exists, so
// that we can just do an early exit from the console.log method
// and similar if we're not going to put any useful output anywhere.
this.ConsoleDivExists = null;
};
// this is an expensive (really quite expensive) string padding function.
// Don't use it for large strings.
console.padString = function(s, padToLength, padCharacter) {
var response = "" + s;
while (response.length < padToLength) {
response = padCharacter + response;
}
return response;
}
console.log = function(message) {
// this will be executed once, on first method invocation, to
// get a reference to the output div if it exists
if (console.ConsoleDivExists == null) {
console.ConsoleDiv = document.getElementById(console.ConsoleDivId);
console.ConsoleDivExists = (console.ConsoleDiv != null);
}
// only do any logging if we actually have an output div.
// (Check using the cached variable so that we don't end up
// with a bunch of failed calls to document.getElementById).
if (console.ConsoleDivExists) {
var date = new Date();
var entireMessage =
console.padString(date.getHours(), 2, "0") + ":" +
console.padString(date.getMinutes(), 2, "0") + ":" +
console.padString(date.getSeconds(), 2, "0") + "." +
console.padString(date.getMilliseconds(), 3, "0") + " " + message;
delete date;
// append the message
console.ConsoleDiv.innerHTML = console.ConsoleDiv.innerHTML + "<br />" + entireMessage;
// scroll the div to the bottom
console.ConsoleDiv.scrollTop = console.ConsoleDiv.scrollHeight;
}
}
}
Ideally you’d drop this into an included script file, but it’s more likely that you’ll paste it into a <script>
tag in the header of your HTML document.
The HTML that creates the DIV to contain the output:
<div id="JavaScriptConsole" style="position: absolute; bottom: 30px; left: 30px; width: 600px; height: 200px; overflow: scroll; background-color: Yellow; color: Red;">
<a href="javascript:document.getElementById('JavascriptConsole').style.visibility = 'hidden';" style="float: right;">Close</a> <span style="font-weight: bold;">JavaScript Console</span><br />
</div>
Note that this div also contains a hyperlink with JavaScript code in it to hide it.
A simple hello world script to log to it:
<script type="text/javascript">
console.log("Hello, world!");
</script>
… and finally, the output: