为调试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 hover 延时器实现代码
Mar 12 Javascript
JavaScript实现点击按钮后变灰避免多次重复提交
Jul 15 Javascript
jquery制作搜狐快站页面效果示例分享
Feb 21 Javascript
jquery判断小数点两位和自动删除小数两位后的数字
Mar 19 Javascript
javascript中返回顶部按钮的实现
May 05 Javascript
Vuejs入门教程之Vue生命周期,数据,手动挂载,指令,过滤器
Apr 19 Javascript
详解AngularJS1.6版本中ui-router路由中/#!/的解决方法
May 22 Javascript
jquery获取transform里的值实现方法
Dec 12 jQuery
浅谈React高阶组件
Mar 28 Javascript
vue 全局环境切换问题
Oct 27 Javascript
jQuery实现验证用户登录
Dec 10 jQuery
关于Node.js中频繁修改代码重启服务器的问题
Oct 15 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
根德Grundig S400/S500/S700电路分析
2021/03/02 无线电
PHP的变量类型和作用域详解
2014/03/12 PHP
PHP扩展开发入门教程
2015/02/26 PHP
PHP5.0~5.6 各版本兼容性cURL文件上传功能实例分析
2018/05/11 PHP
Thinkphp极验滑动验证码实现步骤解析
2020/11/24 PHP
Firefox中beforeunload事件的实现缺陷浅析
2012/05/03 Javascript
jQuery实现仿Alipay支付宝首页全屏焦点图切换特效
2015/05/04 Javascript
jquery实现可自动收缩的TAB网页选项卡代码
2015/09/06 Javascript
js过滤HTML标签完整实例
2015/11/26 Javascript
javascript的 {} 语句块详解
2016/02/27 Javascript
解析JavaScript面向对象概念中的Object类型与作用域
2016/05/10 Javascript
VC调用javascript的几种方法(推荐)
2016/08/09 Javascript
js 性能优化之算法和流程控制
2017/02/15 Javascript
bootstrap Table服务端处理分页(后台是.net)
2017/10/19 Javascript
javascript中的replace函数(带注释demo)
2018/01/07 Javascript
vue.js父子组件通信动态绑定的实例
2018/09/28 Javascript
使用VueRouter的addRoutes方法实现动态添加用户的权限路由
2019/06/03 Javascript
vue渲染方式render和template的区别
2020/06/05 Javascript
JavaScript实现拖拽和缩放效果
2020/08/24 Javascript
vuex页面刷新导致数据丢失的解决方案
2020/12/10 Vue.js
Django中几种重定向方法
2015/04/28 Python
简介Django中内置的一些中间件
2015/07/24 Python
Python解惑之True和False详解
2017/04/24 Python
python对列进行平移变换的方法(shift)
2019/01/10 Python
浅析Python3中的对象垃圾收集机制
2019/06/06 Python
Numpy对数组的操作:创建、变形(升降维等)、计算、取值、复制、分割、合并
2019/08/28 Python
解决Django部署设置Debug=False时xadmin后台管理系统样式丢失
2020/04/07 Python
毕业生个人求职信范例分享
2013/12/17 职场文书
财务人员担保书
2014/05/13 职场文书
捐献物资倡议书范文
2014/05/19 职场文书
中文专业求职信
2014/06/20 职场文书
房产协议书范本2014
2014/09/30 职场文书
2014年远程教育工作总结
2014/12/09 职场文书
2015年五四青年节活动总结
2015/02/10 职场文书
你会写报告?产品体验报告到底该怎么写?
2019/08/14 职场文书
nginx的zabbix 5.0安装部署的方法步骤
2021/07/16 Servers