为调试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 相关文章推荐
如何设置iframe高度自适应在跨域情况下的可用方法
Sep 06 Javascript
javascript中动态函数用法实例分析
May 14 Javascript
javascript获取wx.config内部字段解决微信分享
Mar 09 Javascript
JavaScript中的数组遍历forEach()与map()方法以及兼容写法介绍
May 19 Javascript
jQuery EasyUI菜单与按钮详解
Jul 13 Javascript
jQuery输入框密码的显示隐藏【代码分享】
Apr 29 jQuery
JavaScript定义函数_动力节点Java学院整理
Jun 27 Javascript
详解用vue编写弹出框组件
Jul 04 Javascript
vue中使用localstorage来存储页面信息
Nov 04 Javascript
微信小程序车牌号码模拟键盘输入功能的实现代码
Nov 11 Javascript
jQuery简单实现根据日期计算星期几的方法
Jan 09 jQuery
vue Element-ui表格实现树形结构表格
Jun 07 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
PHP投票系统防刷票判断流程分析
2012/02/04 PHP
PHP读取汉字的点阵数据
2015/06/22 PHP
php执行多个存储过程的方法【基于thinkPHP】
2016/11/08 PHP
关于 Laravel Redis 多个进程同时取队列问题详解
2017/12/25 PHP
PHP实现从PostgreSQL数据库检索数据分页显示及根据条件查找数据示例
2018/06/09 PHP
IE6、IE7中获取Button元素的值的bug说明
2011/08/28 Javascript
读取input:file的路径并显示本地图片的方法
2013/09/23 Javascript
js为空或不是对象问题的快速解决方法
2013/12/11 Javascript
javascript和jquery修改a标签的href属性
2013/12/16 Javascript
使用jQuery设置disabled属性与移除disabled属性
2014/08/21 Javascript
jquery实现标签上移、下移、置顶
2015/04/26 Javascript
jquery序列化方法实例分析
2015/06/10 Javascript
JS阻止事件冒泡行为和闭包的方法
2016/06/16 Javascript
在vue中根据光标的显示与消失实现下拉列表
2019/09/29 Javascript
详解Vue中的Props与Data细微差别
2020/03/02 Javascript
何时/使用 Vue3 render 函数的教程详解
2020/07/25 Javascript
JavaScript事件概念详解(区分静态注册和动态注册)
2021/02/05 Javascript
详解Python3中字符串中的数字提取方法
2017/01/14 Python
python3+PyQt5+Qt Designer实现堆叠窗口部件
2018/04/20 Python
Python中的相关分析correlation analysis的实现
2019/08/29 Python
python2与python3爬虫中get与post对比解析
2019/09/18 Python
Pytorch Tensor 输出为txt和mat格式方式
2020/01/03 Python
ansible动态Inventory主机清单配置遇到的坑
2020/01/19 Python
从多个tfrecord文件中无限读取文件的例子
2020/02/17 Python
python要安装在哪个盘
2020/06/15 Python
pytorch中的weight-initilzation用法
2020/06/24 Python
python爬取”顶点小说网“《纯阳剑尊》的示例代码
2020/10/16 Python
HTML5中通过li-canvas轻松实现单图、多图、圆角图绘制,单行文字、多行文字等
2018/11/30 HTML / CSS
Canvas globalCompositeOperation
2018/12/18 HTML / CSS
追悼会上的答谢词
2014/01/10 职场文书
志愿者活动总结范文
2014/04/26 职场文书
大学班级学风建设方案
2014/05/01 职场文书
校园安全演讲稿
2014/05/09 职场文书
个人承诺书怎么写
2014/05/24 职场文书
2015年社会治安综合治理工作总结
2015/04/10 职场文书
十大最强奥特曼武器:怪兽战斗仪在榜,第五奥特之父只使用过一次
2022/03/18 日漫