I'm working on a jQuery plugin and am trying to make a uniform, cross-browser console object for debugging. The plugin will utilize Firebug or FirebugLite but also allows the user to choose the browser's native console object, if available. For Safari/Chrome, the console methods accept only 1 argument, while the Firebug console API (which is what I'm trying to replicate) accepts any number of arguments that are printed as a concatenated string. I'm using the following code to override the native console log method:

我正在研究一个jQuery插件,我正在尝试制作一个统一的,跨浏览器的控制台对象来进行调试。该插件将使用Firebug或FirebugLite,但也允许用户选择浏览器的本机控制台对象(如果可用)。对于Safari / Chrome,控制台方法只接受1个参数,而Firebug控制台API(我正在尝试复制)接受任意数量的参数,这些参数作为连接字符串打印。我使用以下代码来覆盖本机控制台日志方法:

window.console._log = window.console.log;
window.console.log = function(){
    window.console._log($.makeArray(arguments).join(", "));
};

I have a test page that will fire console.log("str1", "str2") commands on click. The first few times, the log messages are working as expected, but then after some period of time (usually just a few seconds), the log messages begin to only print the first argument. It's as if the console object is being restored to its original, native state and my overridden log() method is being destroyed. Does anyone have any more information on this or might there be a workaround?

我有一个测试页面,可以在点击时激活console.log(“str1”,“str2”)命令。前几次,日志消息按预期工作,但经过一段时间(通常只有几秒钟),日志消息开始只打印第一个参数。就好像控制台对象正在恢复到其原始的本机状态,并且我的被覆盖的log()方法正在被销毁。有没有人有关于此的更多信息或可能有解决方法?

1 个解决方案

#1


Perhaps you might have more luck replacing the entire Console object? That worked for me:

也许您可能有更多的运气替换整个Console对象?这对我有用:

window._console = window.console;
window.console = {
    log: function() {
        window._console.log($.makeArray(arguments).join(", "));
    }
};

FWIW, in the WebCore IDL source, the console property of DOMWindow is marked as Replaceable, whereas the properties of the console object are not.

FWIW,在WebCore IDL源中,DOMWindow的控制台属性被标记为可替换,而控制台对象的属性则不是。

更多相关文章

  1. 如何创建一个npm命令,在控制台中执行两个命令(一个接着一个)?
  2. 错误对象,本机和自定义,如何区分?
  3. 高效地获取XMLhttp对象
  4. javascript面向对象技术基础
  5. Javascript学习:案例7--对象属性和方法的遍历、删除、添加.html
  6. JavaScript String对象方法——replace()
  7. Javascript正则表达式对象和美元符号
  8. 将JavaScript对象转换为要插入关系数据库的数组
  9. Javascript对象

随机推荐

  1. Android获取手机方向
  2. android带图片的AlertDialog和文件管理器
  3. Android Button 点击时替换背景颜色和替
  4. 做了六年Android,终于熬出头了,15K到31K全
  5. Android屏幕自适应的四种方法
  6. Qt for Android(安卓)拉起微信登录、分享
  7. Android(安卓)API Guides---App Manifest
  8. Android应用优化之内存概念
  9. Android跨进程通信
  10. Delphi XE5 for android 调用Java类库必