[英]console.log doesn't work sometimes
I have encountered this several times, & there is no explanation for it. 我已经好几次遇到过这种情况,而且没有任何解释。 There was no JS errors on the site.
网站上没有JS错误。 The code is indeed being executed.
代码确实正在执行。 Replacing them with
alert()
works just fine. 用
alert()
替换它们就可以了。
So today that happened again, so I tried to check the console object in the console object & I found some irregularities as if something has overwritten the console object. 所以今天又发生了,所以我试着检查控制台对象中的控制台对象,我发现了一些不规则的事情,好像有什么东西覆盖了控制台对象。
The following screenshot is of the site (site A) where I have this problem: 以下屏幕截图是我遇到此问题的网站(网站A):
http://ompldr.org/vZ256Mw/Selection_004.jpg http://ompldr.org/vZ256Mw/Selection_004.jpg
You see how log, warn & info are empty functions? 你看到log,warn和info是空函数吗? and the console object is different from how it is on a site (site B) where console works fine (screenshot follows)?
控制台对象与控制台工作正常的网站(站点B)的不同之处(截图如下)?
http://ompldr.org/vZ256Mg/Selection_003.jpg http://ompldr.org/vZ256Mg/Selection_003.jpg
Now the code running on both the sites is exactly same. 现在,在两个站点上运行的代码完全相同。 Site B is my local install and Site A is staging and I don't have anything in the code which overwrites the console object, so what could be an explanation for this behavior?
站点B是我的本地安装,站点A正在升级,我在代码中没有任何覆盖控制台对象的东西,那么这个行为有什么解释呢?
As per nfroidure's suggestion, I added that code in my head section before anything else and I got this: uncaught error! (anonymous function) (anonymous function)
根据nfroidure的建议,我在其他任何事情之前在我的头部添加了代码,我得到了这个:
uncaught error! (anonymous function) (anonymous function)
uncaught error! (anonymous function) (anonymous function)
, second of which points to a code in another library file which has the following in it: uncaught error! (anonymous function) (anonymous function)
,其中第二个指向另一个库文件中的代码,其中包含以下内容:
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() {}
}
This is responsible because I think the if check is faulty. 这是负责任的,因为我认为if检查是错误的。
"console" in window
will be true & "firebug" in window
will be false for me in Chrome resulting in replacing the console methods. "console" in window
将为真,并且在Chrome "firebug" in window
对我来说将是假的,导致替换控制台方法。 It should be &&
over there, no? 那应该是
&&
,不是吗?
Update: After changing the operator to &&
it started to work on Site A, but I don't understand why it only gets triggered on one and not on another. 更新:将运算符更改为
&&
它开始在站点A上运行,但我不明白为什么它只在一个而不是另一个上触发。
看起来Site A有firebug,因为console的方法看起来很像这里的方法: http : //getfirebug.com/wiki/index.php/Console_API
Try to add this on the top of your document : 尝试在文档顶部添加:
<script>
(function(){
var wc=window.console;
delete window.console;
window.__defineGetter__("console", function(){
return wc;
});
window.__defineSetter__("console", function(val){
throw('error!'); // Look at js stack when throwed
});
})();
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.