为调试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获得下拉框值的代码
Aug 13 Javascript
offsetHeight在OnLoad中获取为0的现象
Jul 22 Javascript
jqgrid 表格数据导出实例
Nov 21 Javascript
jquery使用slideDown实现模块缓慢拉出效果的方法
Mar 27 Javascript
JS实现控制表格只显示行边框或者只显示列边框的方法
Mar 31 Javascript
微信小程序 解决请求服务器手机预览请求不到数据的方法
Jan 04 Javascript
大白话讲解JavaScript的Promise
Apr 06 Javascript
简述vue路由打开一个新的窗口的方法
Nov 29 Javascript
js中call()和apply()改变指针问题的讲解
Jan 17 Javascript
详解VUE单页应用骨架屏方案
Jan 17 Javascript
layui自定义插件citySelect实现省市区三级联动选择
Jul 26 Javascript
浅谈vue中$event理解和框架中在包含默认值外传参
Aug 07 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网站安装程序制作的原理、步骤、注意事项和示例代码
2010/08/01 PHP
PHP Session机制简介及用法
2014/08/19 PHP
ThinkPHP整合百度Ueditor图文教程
2014/10/21 PHP
PHP实现的构造sql语句类实例
2016/02/03 PHP
PHP addAttribute()函数讲解
2019/02/03 PHP
Jquery下的26个实用小技巧(jQuery tips, tricks &amp; solutions)
2010/03/01 Javascript
JS Range HTML文档/文字内容选中、库及应用介绍
2011/05/12 Javascript
jquery插件开发之实现google+圈子选择功能
2014/03/10 Javascript
jQuery表格排序组件-tablesorter使用示例
2014/05/26 Javascript
js获取鼠标位置实例详解
2015/12/09 Javascript
vue-router判断页面未登录自动跳转到登录页的方法示例
2018/11/04 Javascript
JavaScript数据结构与算法之二叉树遍历算法详解【先序、中序、后序】
2019/02/21 Javascript
Element Dialog对话框的使用示例
2020/07/26 Javascript
Python的Asyncore异步Socket模块及实现端口转发的例子
2016/06/14 Python
Python中的__slots__示例详解
2017/07/06 Python
浅谈Python处理PDF的方法
2017/11/10 Python
python实现kMeans算法
2017/12/21 Python
使用python将请求的requests headers参数格式化方法
2019/01/02 Python
对Python实现累加函数的方法详解
2019/01/23 Python
Django使用Channels实现WebSocket的方法
2019/07/28 Python
pyenv与virtualenv安装实现python多版本多项目管理
2019/08/17 Python
python实现树的深度优先遍历与广度优先遍历详解
2019/10/26 Python
python3实现单目标粒子群算法
2019/11/14 Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
2020/04/13 Python
Django 5种类型Session使用方法解析
2020/04/29 Python
Python3通过chmod修改目录或文件权限的方法示例
2020/06/08 Python
泰国折扣酒店预订:Hotels2Thailand
2018/03/20 全球购物
C#可否对内存进行直接的操作
2015/02/26 面试题
药学专业毕业生求职信
2013/10/20 职场文书
工厂保安员岗位职责
2014/01/31 职场文书
爱我中华演讲稿
2014/05/20 职场文书
历史学专业求职信
2014/06/19 职场文书
商场促销活动策划方案
2014/08/18 职场文书
学校领导四风问题整改措施思想汇报
2014/10/09 职场文书
单位实习鉴定评语
2015/01/04 职场文书
中学推普周活动总结
2015/05/07 职场文书