【问题标题】:Javascript Console.log Not working in Chrome, or FirefoxJavascript Console.log 在 Chrome 或 Firefox 中不起作用
【发布时间】:2023-03-08 10:50:01
【问题描述】:

我做的很简单:

console.log("Testing");

还有:

alert("testing");

警报有效(所以我知道 javascript 有效)但我看不到日志。当我使用 Firefox 时,我收到以下错误:

The Web Console logging API (console.log, console.info, console.warn, console.error) has been disabled by a script on this page.

发生了什么事?我查看了以下主题,但没有任何帮助:

Chrome: console.log, console.debug are not working

console.log quit working in Chrome

Console.log not working in Chrome [closed]

why does console.log not output in chrome? Console.log not working at all

我还确保漏斗正常工作并且日志记录已打开。

还有什么问题?

【问题讨论】:

  • 打开控制台输入console.toString()返回什么?我敢打赌它是一个函数而不是一个对象。 AKA 有人覆盖了控制台。
  • 我认为这很好地解释了它:“Web 控制台日志记录 API(console.log、console.info、console.warn、console.error)已被此脚本禁用page" 找出禁用它的脚本。
  • @epascarello 在 Chrome 和 Firefox 上没有返回任何内容
  • @KevinB 考虑到我在使用 CHROME 时遇到了这个问题,我不得不不同意这是重复的。

标签: javascript wordpress google-chrome firefox logging


【解决方案1】:

我刚刚在 Firefox 更新后遇到了这个问题并设法修复了它。这是导致问题的代码:

/* IE fix that allows me to still log elsewhere */
if (typeof(console)=="undefined") {
    var console = {
        output: null,
        log: function (str) {
            // we can't emulate the console in IE, but we can cache what's output
            // so that IE users can get it via console.output
            if (!this.output) this.output = new Array();
            this.output.push(new String(str));
        }
    };
    window.console = console;
}

在以前版本的 FireFox 中,“var console;”不会被执行。现在它似乎添加了某种分支/预测机制。看到我可能定义了一个具有全局范围的名为console的变量,它禁用了window.console

我通过将 var console; 重命名为 var cons;

解决了这个问题
/* IE fix that allows me to still log elsewhere */
if (typeof(console)=="undefined") {
    var cons = {
        output: null,
        log: function (str) {
            // we can't emulate the console in IE, but we can cache what's output
            // so that IE users can get it via console.output
            if (!this.output) this.output = new Array();
            this.output.push(new String(str));
        }
    };
    window.console = cons;
}

不过,我仍然需要对此进行测试,以确保它在 IE 中符合我的预期。我只需要找到一个没有控制台的IE副本(我认为是9或以下)。

我只是希望 Firefox 能告诉你什么脚本的哪一行禁用了控制台——那太好了。

【讨论】:

    【解决方案2】:

    我在使用 Magento 电子商务网站(版本 1.6.2.0)时遇到了同样的问题。

    我修复了它在/js/varien/js.js:637 中注释以下行

    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() {}
    }
    

    此修复(显然)仅适用于 Magento 网站。

    【讨论】:

      【解决方案3】:

      原来主题开发者在我不知情的情况下在主题中添加了 firebug lite。关闭它可以解决问题。

      【讨论】:

        【解决方案4】:

        我认为您在尝试通过console.log() 登录时正在查看的页面中有一个脚本,该脚本会覆盖属性window.console.log。通常这个属性是浏览器预设的一个函数,但是你的脚本可能会覆盖它。

        【讨论】:

          【解决方案5】:

          第一个建议:您是否使用任何其他使用控制台的开发工具?在 Firefox 上,我在后台运行 Firebug 时遇到了同样的问题,而我没有注意到它,在关闭 firebug 后,错误消失了。这是一个可能重复的线程:Firefox Web Console Disabled?

          其次,如果它被某些脚本覆盖,则一个一个禁用脚本,看看哪个会导致错误。

          【讨论】:

          • 我没有安装 firebug。
          猜你喜欢
          • 2013-03-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-08
          • 1970-01-01
          相关资源
          最近更新 更多