JavaScript 控制台登录 Magento

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8080610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:11:10  来源:igfitidea点击:

JavaScript console log in Magento

javascriptmagentoconsoleloggingconflict

提问by enloz

I have a custom phtml pages in Magento. As far I know Magento uses jQuery and prototype libraries.

我在 Magento 中有一个自定义的 phtml 页面。据我所知,Magento 使用 jQuery 和原型库。

For example, if I need external jQuery/jQueryUI, I need to use .noConflict()

例如,如果我需要外部 jQuery/jQueryUI,我需要使用.noConflict()

But if I want to use

但是如果我想使用

console.log('Hello world');

In Chrome 15 console I got no response, nothing. Also tried with Firebug.

在 Chrome 15 控制台中,我没有任何响应,什么也没有。也尝试过 Firebug。

Obviously there is some conflict with Magento JavaScript code. Is there any solution?

显然与 Magento JavaScript 代码存在一些冲突。有什么解决办法吗?

回答by sg3s

So in light of not wanting to smear this site with profanity I will just say someone wasn't thinking in the magento team or somehow some crappy code got into live releases....

因此,鉴于不想用亵渎神灵来抹黑这个网站,我只想说 magento 团队中有人没有考虑过,或者一些蹩脚的代码以某种方式进入了实时发布......

If your console.log()is not working on a Magento installation it is likely because of the following:

如果您console.log()没有安装 Magento,可能是由于以下原因:

In magento/js/varien/js.js@ line ~636, Magento ver. 1.6.2.0

magento/js/varien/js.js@ line ~636,Magento 版本。1.6.2.0

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 effectively prevents console.log()from working in any browser other than firefox with firebug.

这有效地防止console.log()了在带有 firebug 的 firefox 之外的任何浏览器中工作。

To protect IE, surely, but I think this is the wrong way to get arround it, instead people should be aware of what they do with their logging and face the consequences when they don't.

当然,为了保护 IE,但我认为这是绕过它的错误方法,相反,人们应该意识到他们对日志做了什么,并在不这样做时面对后果。

To fix it just make sure you put delete window['console'];(javascript) before you try to do a console.log(), or if you don't mind modifying the core files, delete the code above.

要修复它,请确保delete window['console'];在尝试执行 a 之前放入(javascript) console.log(),或者如果您不介意修改核心文件,请删除上面的代码。

Please note: remove the console fix for production, the delete doesn't work in IE6-8 and throws an error

请注意:删除生产中的控制台修复,删除在 IE6-8 中不起作用并引发错误

回答by Anthony

Adding this layout update in your app/design/frontend/default/default/layout/local.xmlor your theme's app/design/frontend/default/default/layout/page.xmlin the <default>handle is the cleanest, most direct way to add back the console object in all browsers on all pages.

在您添加此布局更新app/design/frontend/default/default/layout/local.xml或你的主题app/design/frontend/default/default/layout/page.xml<default>手柄是在所有页面上所有的浏览器添加回控制台对象最清洁,最直接的方式。

<default>
    <reference name="content">
        <block type="core/text" name="fix.console" as="fix.console">
            <action method="setText">
                <text><![CDATA[<script type="text/javascript">
                    iframe                  = document.createElement('iframe');
                    iframe.style.display    = 'none';
                    document.getElementsByTagName('body')[0].appendChild(iframe);
                    window.console          = iframe.contentWindow.console;
                    console.firebug         = "faketrue";                   
                </script>]]></text>
            </action>
        </block>
    </reference>
</default>

回答by haifacarina

This is a quick fix.

这是一个快速修复。

jQuery(document).ready(function(){
   window.console = jQuery('<iframe>').hide().appendTo('body')[0].contentWindow.console;
});

Source: http://updownleftright.net/blog/2011/09/javascript-tip-of-the-day-restoring-console-log-on-a-magento-site

来源:http: //updownleftright.net/blog/2011/09/javascript-tip-of-the-day-restoring-console-log-on-a-magento-site

回答by Alex Beauchemin

In the file js.js there is this code :

在文件 js.js 中有这样的代码:

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

So what it actually does, if the console is not the firebug console (in firefox) , it deactivate it. So in the built in console of google chrome, it doesn't work.

那么它的实际作用是,如果控制台不是 firebug 控制台(在 firefox 中),它将停用它。所以在谷歌浏览器的内置控制台中,它不起作用。

There is 2 options : Use firefox with firebug , or remove this block of code.

有 2 个选项:将 firefox 与 firebug 一起使用,或删除此代码块。

回答by witherspoon

Why not check if the Console object is defined first?

为什么不先检查 Console 对象是否已定义?

Instead of:

代替:

if (!("console" in window) || !("firebug" in console))

You could write:

你可以写:

if( typeof console === 'undefined' )

回答by Tobias Hagenbeek

All you need to do before you console log the first time on the page.

在您第一次登录页面之前,您需要做的所有事情。

 delete window['console'];

回答by Jürgen Thelen

Using console.log()on browsers using Firebug 1.9.0+ with Magento up to 1.6.2.0 will fail, because Magento checks for the console.firebugproperty, but this property has been removed with Firebug 1.9.0for privacy reasons.

使用console.log()上使用Firebug 1.9.0+与Magento的高达1.6.2.0将失败的浏览器,因为Magento的检查的console.firebug属性,但这种属性已使用Firebug 1.9.0删除隐私的原因。

See the file js/varien/js.js:

查看文件js/varien/js.js

if (!("console" in window) || !("firebug" in console))
{
    // :
}

Since Magento 1.7.0.0 this whole condition is commented out to fix this (and other) issue(s).

从 Magento 1.7.0.0 开始,整个条件被注释掉以解决这个(和其他)问题。

回答by kalenjordan

This is no longer an issue as of latest version in the Mage core. The code that breaks console.log()is commented out now. I'm not sure exactly in which version it was fixed, but it's fixed as of CE 1.7.0.2.

从 Mage 核心的最新版本开始,这不再是问题。中断的代码console.log()现在被注释掉了。我不确定它是在哪个版本中修复的,但它从 CE 1.7.0.2 开始修复。

回答by Sam Cambridge

After AlexB post I used this work around.

在 AlexB 发布后,我使用了这项工作。

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

if (!("console" in window) || !("firebug" in console) && !is_chrome)
{
    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() {}
}

As you can see, the is_chrome var returns true or false, adding !is_chrome stops the code from running.

如您所见,is_chrome var 返回 true 或 false,添加 !is_chrome 会阻止代码运行。