为调试JavaScript添加输出窗口的代码


Posted in Javascript onFebruary 07, 2010

虽然不是很复杂的实现,但每次都要这样就会很麻烦,所以我写了一小段脚本,用来自动生成这个输出窗口。
代码

window.Babu = {}; 
Babu.Debugging = {}; 
Babu.Debugging.writeLine = function(format, arg1, arg2) { 
var console = Babu.Debugging._getConsole(); 
if (console.get_visible()) { 
var msg = format; 
if (typeof msg !== "undefined" && msg !== null) { 
var index; 
if (typeof msg === "string") { 
var array = format.match(/\{(\d+)\}/g); 
if (array) { 
for (var i = 0; i < array.length; i++) { 
index = array[i]; 
index = parseInt(index.substr(1, index.length - 2)) + 1; 
msg = msg.replace(array[i], arguments[index]); 
} 
} 
} 
} 
var span = document.createElement("SPAN"); 
span.appendChild(document.createTextNode(msg)); 
console._output.appendChild(span); 
console._output.appendChild(document.createElement("BR")); 
span.scrollIntoView(); 
return span; 
} 
} 
Babu.Debugging._getConsole = function() { 
var console = Babu.Debugging._console; 
if (!console) { 
var div = document.createElement("DIV"); 
div.style.position = "fixed"; 
div.style.right = "3px"; 
div.style.bottom = "3px"; 
div.style.width = "350px"; 
div.style.height = "180px"; 
div.style.backgroundColor = "white"; 
div.style.color = "black"; 
div.style.border = "solid 2px #afafaf"; 
div.style.fontSize = "12px"; 
document.body.appendChild(div); 
Babu.Debugging._console = console = div; 
div = document.createElement("DIV"); 
div.style.backgroundColor = "#e0e0e0"; 
div.style.position = "absolute"; 
div.style.left = "0px"; 
div.style.right = "0px"; 
div.style.top = "0px"; 
div.style.height = "16px"; 
div.style.padding = "2px 2px"; 
div.style.margin = "0px"; 
console.appendChild(div); 
console._toolbar = div; 
div = document.createElement("DIV"); 
div.style.overflow = "auto"; 
div.style.whiteSpace = "nowrap"; 
div.style.position = "absolute"; 
div.style.left = "0px"; 
div.style.right = "0px"; 
div.style.top = "20px"; 
div.style.bottom = "0px"; 
div.style.height = "auto"; 
console.appendChild(div); 
console._output = div; 
var btn; 
btn = document.createElement("SPAN"); 
btn.innerHTML = "收缩"; 
btn.style.margin = "0px 3px"; 
btn.style.cursor = "pointer"; 
console._toolbar.appendChild(btn); 
btn.onclick = function() { if (console.get_collapsed()) console.expand(); else console.collapse(); } 
btn = document.createElement("SPAN"); 
btn.innerHTML = "清除"; 
btn.style.margin = "0px 3px"; 
btn.style.cursor = "pointer"; 
console._toolbar.appendChild(btn); 
btn.onclick = Babu.Debugging.clearConsole; 
btn = document.createElement("SPAN"); 
btn.innerHTML = "关闭"; 
btn.style.cursor = "pointer"; 
btn.style.margin = "0px 3px"; 
console._toolbar.appendChild(btn); 
btn.onclick = function() { Babu.Debugging.hideConsole(); } 
console.get_visible = function() { return this.style.display !== "none" }; 
console.get_collapsed = function() { return !(!this._collapseState) }; 
console.collapse = function() { 
if (!this.get_collapsed()) { 
this._output.style.display = "none"; 
this._toolbar.childNodes[1].style.display = "none"; 
this._toolbar.childNodes[2].style.display = "none"; 
this._toolbar.childNodes[0].innerHTML = "展开"; 
this._collapseState = { width: this.style.width, height: this.style.height } 
this.style.width = "30px"; 
this.style.height = "16px"; 
} 
} 
console.expand = function() { 
if (this.get_collapsed()) { 
this._output.style.display = ""; 
this._toolbar.childNodes[1].style.display = ""; 
this._toolbar.childNodes[2].style.display = ""; 
this._toolbar.childNodes[0].innerHTML = "收缩"; 
this.style.width = this._collapseState.width; 
this.style.height = this._collapseState.height; 
this._collapseState = null; 
} 
} 
} 
return console; 
} 
Babu.Debugging.showConsole = function() { 
Babu.Debugging._getConsole().style.display = ""; 
} 
Babu.Debugging.hideConsole = function() { 
var console = Babu.Debugging._console; 
if (console) { 
console.style.display = "none"; 
} 
} 
Babu.Debugging.clearConsole = function() { 
var console = Babu.Debugging._console; 
if (console) console._output.innerHTML = ""; 
}

调用方法很简单:
Babu.Debugging.writeLine("调试信息"); 
Babu.Debugging.writeLine("带参数的调试信息:参数1={0},参数2={1}", arg1, arg2);

调用之后,会自动在窗口的右下角出现一个固定位置的窗口,并显示相应的内容。效果图请看下面:
为调试JavaScript添加输出窗口的代码
Javascript 相关文章推荐
jQuery中与toggleClass等价的程序段 以及未来学习的方向
Mar 18 Javascript
JavaScript高级程序设计(第3版)学习笔记6 初识js对象
Oct 11 Javascript
js跑马灯代码(自写)
Apr 17 Javascript
JavaScript中setUTCFullYear()方法的使用简介
Jun 12 Javascript
Jquery中使用show()与hide()方法动画显示和隐藏图片
Oct 08 Javascript
为何JS操作的href都是javascript:void(0);呢
Nov 12 Javascript
jQuery on()方法绑定动态元素的点击事件实例代码浅析
Jun 16 Javascript
更靠谱的H5横竖屏检测方法(js代码)
Sep 13 Javascript
jquery-mobile基础属性与用法详解
Nov 23 Javascript
JavaScript前端实现压缩图片功能
Mar 06 Javascript
vue实现在线学生录入系统
May 30 Javascript
vue 中this.$set 动态绑定数据的案例讲解
Jan 29 Vue.js
JavaScript 读取元素的CSS信息的代码
Feb 07 #Javascript
js png图片(有含有透明)在IE6中为什么不透明了
Feb 07 #Javascript
JavaScript Event学习第八章 事件的顺序
Feb 07 #Javascript
JavaScript Event学习第七章 事件属性
Feb 07 #Javascript
JavaScript Event学习第六章 事件的访问
Feb 07 #Javascript
JavaScript Event学习第五章 高级事件注册模型
Feb 07 #Javascript
JavaScript Event学习第四章 传统的事件注册模型
Feb 07 #Javascript
You might like
使用sockets:从新闻组中获取文章(二)
2006/10/09 PHP
PHP中的正规表达式(一)
2006/10/09 PHP
php中文本操作的类
2007/03/17 PHP
php生成txt文件标题及内容的方法
2014/01/16 PHP
PHP实现的简单异常处理类示例
2017/05/04 PHP
JavaScript 快捷键设置实现代码
2009/03/13 Javascript
jquery 最简单的属性菜单
2009/10/08 Javascript
JavaScript高级程序设计 XML、Ajax 学习笔记
2011/09/10 Javascript
通过修改360抢票的刷新频率和突破8车次限制实现方法
2017/01/04 Javascript
微信小程序开发(一) 微信登录流程详解
2017/01/11 Javascript
JS实现利用两个队列表示一个栈的方法
2017/12/13 Javascript
angular2实现统一的http请求头方法
2018/08/13 Javascript
详解angular应用容器化部署
2018/08/14 Javascript
koa源码中promise的解读
2018/11/13 Javascript
JS把字符串格式的时间转换成几秒前、几分钟前、几小时前、几天前等格式
2019/07/10 Javascript
JS回调函数原理与用法详解【附PHP回调函数】
2019/07/20 Javascript
图解JS原型和原型链实现原理
2020/09/15 Javascript
Python函数参数类型*、**的区别
2015/04/11 Python
将Python中的数据存储到系统本地的简单方法
2015/04/11 Python
Python字典操作简明总结
2015/04/13 Python
Pyhthon中使用compileall模块编译源文件为pyc文件
2015/04/28 Python
python 系统调用的实例详解
2017/07/11 Python
Python requests模块实例用法
2019/02/11 Python
详解Python locals()的陷阱
2019/03/26 Python
python3 pillow模块实现简单验证码
2019/10/31 Python
Python连接Oracle之环境配置、实例代码及报错解决方法详解
2020/02/11 Python
使用Keras中的ImageDataGenerator进行批次读图方式
2020/06/17 Python
毕业生应聘幼儿园的自荐信
2013/11/20 职场文书
医学院校毕业生自荐信范文
2014/01/01 职场文书
2014两会学习心得:榜样精神伴我行
2014/03/17 职场文书
八项规定自查自纠报告及整改措施
2014/10/26 职场文书
胡雪岩故居导游词
2015/02/06 职场文书
2015社区爱国卫生工作总结
2015/04/21 职场文书
同步小康驻村工作简报
2015/07/20 职场文书
公司借款担保书
2015/09/22 职场文书
2019职场实习报告该怎么写?
2019/07/01 职场文书