What happened to console.log in IE8?
Even better for fallback is this:
var alertFallback = true;
if (typeof console === "undefined" || typeof console.log === "undefined") {
console = {};
if (alertFallback) {
console.log = function(msg) {
alert(msg);
};
} else {
console.log = function() {};
}
}
Console.log in IE8
you can prevent ie8 errors on console.log with
if (!window.console){ console = {log: function() {}} };
Console undefined issue in IE8
You could add the following to the if clause:
if (console && console.log) {
console.log('removing child');
}
Or write a log wrapper around the console.log function like this.
window.log = function () {
if (this.console && this.console.log) {
this.console.log(Array.prototype.slice.call(arguments));
}
}
Use it like this:
log("This method is bulletproof", window, arguments");
And here is a jsfiddle for this:
http://jsfiddle.net/joquery/4Ugvg/
Console undefined issue in IE8
You could add the following to the if clause:
if (console && console.log) {
console.log('removing child');
}
Or write a log wrapper around the console.log function like this.
window.log = function () {
if (this.console && this.console.log) {
this.console.log(Array.prototype.slice.call(arguments));
}
}
Use it like this:
log("This method is bulletproof", window, arguments");
And here is a jsfiddle for this:
http://jsfiddle.net/joquery/4Ugvg/
How can I use console logging in Internet Explorer?
You can access IE8 script console by launching the "Developer Tools" (F12). Click the "Script" tab, then click "Console" on the right.
From within your JavaScript code, you can do any of the following:
<script type="text/javascript">
console.log('some msg');
console.info('information');
console.warn('some warning');
console.error('some error');
console.assert(false, 'YOU FAIL');
</script>
Also, you can clear the Console by calling console.clear()
.
NOTE: It appears you must launch the Developer Tools first then refresh your page for this to work.
How can I use console.log on IE by using Modernizr.js
You may create a simple console polyfill for IE:
window.console = window.console || {
log: function () {}
};
This won't have any effect on modern browsers. It'll just prevent undefined console error in IE.
What would cause a Message: 'console' is undefined error to arise all of a sudden?
IE 8 will always throw that error if there is a call to console.log
when the Developer Tools are not open.
Essentially, to IE 8, it is a null reference until the console is open.
Does IE9 support console.log, and is it a real function?
In Internet Explorer 9 (and 8), the console
object is only exposed when the developer tools are opened for a particular tab. If you hide the developer tools window for that tab, the console
object remains exposed for each page you navigate to. If you open a new tab, you must also open the developer tools for that tab in order for the console
object to be exposed.
The console
object is not part of any standard and is an extension to the Document Object Model. Like other DOM objects, it is considered a host object and is not required to inherit from Object
, nor its methods from Function
, like native ECMAScript functions and objects do. This is the reason apply
and call
are undefined on those methods. In IE 9, most DOM objects were improved to inherit from native ECMAScript types. As the developer tools are considered an extension to IE (albeit, a built-in extension), they clearly didn't receive the same improvements as the rest of the DOM.
For what it's worth, you can still use some Function.prototype
methods on console
methods with a little bind()
magic:
var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]);
//-> "thisisatest"
console' is undefined error for Internet Explorer
Try
if (!window.console) console = ...
An undefined variable cannot be referred directly. However, all global variables are attributes of the same name of the global context (window
in case of browsers), and accessing an undefined attribute is fine.
Or use if (typeof console === 'undefined') console = ...
if you want to avoid the magic variable window
, see @Tim Down's answer.
Related Topics
Why Is It Necessary to Set the Prototype Constructor
Calling a Parent Window Function from an Iframe
How to Parse a CSV String With JavaScript, Which Contains Comma in Data
How to Match a Whole Word in JavaScript
How to Remove Item from Array by Value
For Loop For Htmlcollection Elements
Merge Two Array of Objects Based on a Key
JavaScript Function to Add X Months to a Date
Why Does String to Number Comparison Work in JavaScript
Jquery $(Document).Ready and Updatepanels
Angularjs Routing Without the Hash '#'
Get/Set Current @Keyframes Percentage/Change Keyframes
Google Map Custom Marker with CSS Rounded Corner
Span Inside Button, Is Not Clickable in Firefox
Call Java Function from JavaScript Over Android Webview
Detecting Touch Screen Devices with JavaScript