有没有办法登录控制台而不破坏IE下的代码? [英] Is there a way to log to console without breaking code under IE?

查看:85
本文介绍了有没有办法登录控制台而不破坏IE下的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用console.log将一些日志记录放入我程序的javascript端。但是我注意到,除非在IE中打开开发控制台,否则JS在命中console.log时基本停止工作。这是一个痛苦......这意味着每当我想进行生产构建时,我必须删除所有日志记录。

I'm trying to use console.log to put some logging into the javascript side of my program. I noticed, though, that unless the dev console is open in IE, JS basically stops working when it hits console.log. This is a pain... it means I have to remove all the logging whenever I want to do a production build.

除了显而易见的:

function DoSafeConsoleLog( parameters )
{
    if ( !$.browser.msie )
    {
         console.log( parameters ); 
    }
}

有一个很好的方法来记录友好的javascript到所有主流浏览器?

is there a good way to log javascript that is friendly to all major browsers?

编辑:

嗯,看完重复的帖子(oops)之后考虑到这里的答案,我必须在调用之前检查控制台是否存在。尽管我不愿意获得额外的标记,但我宁愿不要踩到可能想要使用Firebug Lite调试代码的未来程序员。

Well, after looking at the duplicate post (oops) as well as considering the answers here, I've gotta side with just checking for the existence of console before calling. Even though I am loathe to have the extra markup, I would rather not step on the feet of future programmers who might want to use Firebug Lite to debug my code.

推荐答案

IE有自己的控制台,如果你正在使用 firebug lite 。只需确保在调用日志时控制台存在:

IE has its own console, and you wont want to override console if you're using firebug lite. Just make sure that console exists when log gets called:

if (window.console) console.log('foo bar baz', fizz, buzz);

更好的是,使用&& 快捷方式:

Better yet, use && to shortcut:

window.console && console.log('foo bar baz', fizz, buzz);

这篇关于有没有办法登录控制台而不破坏IE下的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆