为调试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 相关文章推荐
用js遍历 table的脚本
Jul 23 Javascript
javascript 面向对象继承
Nov 26 Javascript
鼠标右击事件代码(asp.net后台)
Jan 27 Javascript
js内存泄露的几种情况详细探讨
May 31 Javascript
JS批量修改PS中图层名称的方法
Jan 26 Javascript
js 获取页面高度和宽度兼容 ie firefox chrome等
May 14 Javascript
javascript实现根据身份证号读取相关信息
Dec 17 Javascript
js中setTimeout()与clearTimeout()用法实例浅析
May 12 Javascript
EasyUI创建对话框的两种方式
Aug 23 Javascript
jQuery实现简单的tab标签页效果
Sep 12 Javascript
jQuery中hover方法搭配css的hover选择器,实现选中元素突出显示方法
May 08 jQuery
原生js实现省市区三级联动代码分享
Feb 12 Javascript
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 静态页面中显示动态内容
2009/08/14 PHP
php文件操作相关类实例
2015/06/18 PHP
PHP使用MPDF类生成PDF的方法
2015/12/08 PHP
PHPCMS忘记后台密码的解决办法
2016/10/30 PHP
php写app接口并返回json数据的实例(分享)
2017/05/20 PHP
Thinkphp5+plupload实现的图片上传功能示例【支持实时预览】
2019/05/08 PHP
javascript div 遮罩层封锁整个页面
2009/07/10 Javascript
JS中confirm,alert,prompt函数使用区别分析
2010/04/01 Javascript
Date对象格式化函数代码
2010/07/17 Javascript
关于jquery ajax 调用带参数的webservice返回XML数据一个小细节
2012/07/31 Javascript
DWZ table的原生分页浅谈
2013/03/01 Javascript
javascript中的Function.prototye.bind
2015/06/25 Javascript
Angular设置title信息解决SEO方面存在问题
2016/08/19 Javascript
javascript操作cookie
2017/01/17 Javascript
Angularjs单选框相关的示例代码
2017/08/17 Javascript
React Native之TextInput组件解析示例
2017/08/22 Javascript
VueJs 搭建Axios接口请求工具
2017/11/20 Javascript
vue.js前后端数据交互之提交数据操作详解
2018/04/24 Javascript
Vue实现简单计算器案例
2020/02/25 Javascript
解决vue axios跨域 Request Method: OPTIONS问题(预检请求)
2020/08/14 Javascript
[42:52]Optic vs Serenity 2018国际邀请赛淘汰赛BO3 第二场 8.22
2018/08/23 DOTA
Python函数返回值实例分析
2015/06/08 Python
Django 后台获取文件列表 InMemoryUploadedFile的例子
2019/08/07 Python
python Jupyter运行时间实例过程解析
2019/12/13 Python
详解pytorch tensor和ndarray转换相关总结
2020/09/03 Python
Farfetch阿联酋:奢侈品牌时尚购物平台
2019/07/26 全球购物
英国打印机墨盒销售网站:Ink Factory
2019/10/07 全球购物
请说出你所知道的线程同步的方法
2013/04/19 面试题
表彰先进的通报
2014/01/31 职场文书
采购部经理岗位职责
2014/02/10 职场文书
西式结婚主持词
2014/03/14 职场文书
会计员岗位职责
2014/03/15 职场文书
法学院毕业生求职信
2014/06/25 职场文书
社区个人对照检查材料(群众路线)
2014/09/26 职场文书
2014年基层党支部工作总结
2014/12/04 职场文书
2015年优质护理服务工作总结
2015/04/08 职场文书