在哪些情況下,在Internet Explorer 9中定義了window.console.log
?IE9是否支持console.log,它是一個真正的功能?
即使定義了window.console.log
,window.console.log.apply
和window.console.log.call
也未定義。爲什麼是這樣?
[用於IE8相關問題:What happened to console.log in IE8?]
在哪些情況下,在Internet Explorer 9中定義了window.console.log
?IE9是否支持console.log,它是一個真正的功能?
即使定義了window.console.log
,window.console.log.apply
和window.console.log.call
也未定義。爲什麼是這樣?
[用於IE8相關問題:What happened to console.log in IE8?]
在Internet Explorer 9(和圖8),當顯影劑工具打開,以便一個特定標籤的console
對象僅暴露。如果您隱藏該選項卡的開發人員工具窗口,則對於您導航到的每個頁面,console
對象都將保持公開。如果您打開一個新選項卡,則還必須打開該選項卡的開發人員工具才能顯示console
對象。
console
對象不是任何標準的一部分,並且是文檔對象模型的擴展。像其他DOM對象一樣,它被認爲是主機對象,並不需要像本地ECMAScript函數和對象那樣繼承Object
,也不需要從Function
繼承它的方法。這就是在這些方法上未定義apply
和call
的原因。在IE 9中,大多數DOM對象都得到了改進,可以從本機ECMAScript類型繼承。由於開發人員工具被認爲是IE的擴展(雖然是內置擴展),但他們顯然沒有得到與DOM其餘部分相同的改進。
對於它的價值,你仍然可以使用上console
方法的一些Function.prototype
方法有一點bind()
法寶:
var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]);
//-> "thisisatest"
對於Firebug的'console'對象也是如此。 – 2011-03-29 13:36:43
我可以不自豪地說,我爲web開發多年,我認爲所有主流瀏覽器都支持console.log。我花了一天時間研究爲什麼IE9不喜歡我的腳本,現在我知道爲什麼 - 它在第一步中有一個console.log。無法調試,因爲轉向調試模式使這個錯誤瞬間消失:P謝謝澄清! – f055 2012-07-16 22:00:01
昨天有同樣的問題。安裝[DebugBar](http://www.debugbar.com/)碰巧幫助我更快,因爲它沒有定義控制檯對象。所以,當我隱藏IE控制檯而不是DebugBar時,我從後者得到一條消息,說明存在JavaScript錯誤(控制檯未定義)。 – 2012-09-28 05:21:46
一個簡單的解決這個問題的console.log是確定之初以下你的JS代碼:
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function() { };
這適用於所有瀏覽器。這會在調試器未處於活動狀態時爲console.log創建一個虛擬函數。當調試器處於活動狀態時,將定義方法console.log並正常執行。
......怎麼
console = { log : function(text) { alert(text); } }
在某些情況下,這可能是一個可以通過的解決方法,但實際上並未解決該問題。 – 2015-01-19 21:49:57
閱讀從上面馬克Cliament的評論文章後,我現在已經改變了我的通用跨瀏覽器的console.log功能看起來像這樣:
function log()
{
"use strict";
if (typeof(console) !== "undefined" && console.log !== undefined)
{
try
{
console.log.apply(console, arguments);
}
catch (e)
{
var log = Function.prototype.bind.call(console.log, console);
log.apply(console, arguments);
}
}
}
只是'Function.prototype.apply.call(console.log,控制檯,參數);' – Victor 2013-10-29 09:52:33
@Victor它絕對應該是唯一一個被接受的答案! – 2015-03-11 12:41:10
console.log僅在控制檯打開時定義。如果你想在你的代碼來檢查它確保您的窗口屬性
if (window.console)
console.log(msg)
,這將引發一個例外IE9,將無法正常工作範圍內檢查它。不要這樣做
if (console)
console.log(msg)
我知道這是一個非常古老的問題,但感覺這增加了如何處理控制檯問題的有價值的選擇。在調用控制檯之前放置以下代碼*(這是您的第一個腳本)。
// Avoid `console` errors in browsers that lack a console.
(function() {
var method;
var noop = function() {};
var methods = [
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
'timeStamp', 'trace', 'warn'
];
var length = methods.length;
var console = (window.console = window.console || {});
while (length--) {
method = methods[length];
// Only stub undefined methods.
if (!console[method]) {
console[method] = noop;
}
}
}());
參考:
https://github.com/h5bp/html5-boilerplate/blob/v5.0.0/dist/js/plugins.js
我想提一提,如果你使用的console.log與開發工具的所有版本的Windows關閉IE9不會引發錯誤。在XP上它確實,但在Windows 7上卻沒有。 所以,如果你放棄了對WinXP的支持,你可以直接使用console.log。
不正確仍然是Windows 7上的問題 – 2017-03-08 21:15:35
查看關於IE8-9控制檯對象/函數錯綜複雜的好帖子:http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object – 2011-12-30 13:37:51
另請參閱['console'是Internet Explorer的未定義錯誤](http://stackoverflow.com/q/3326650/1048572) – Bergi 2012-11-10 12:34:03