为调试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 1.0.4 - New Wave Javascript(js源文件)
Jan 15 Javascript
Jquery乱码的一次解决过程 图解教程
Feb 20 Javascript
JS控制阿拉伯数字转为中文大写示例代码
Sep 04 Javascript
jquery简单图片切换显示效果实现方法
Jan 14 Javascript
使用JavaScript脚本无法直接改变Asp.net中Checkbox控件的Enable属性的解决方法
Sep 16 Javascript
AngularJS实现ajax请求的方法
Nov 22 Javascript
Javascript 使用ajax与C#获取文件大小实例详解
Jan 13 Javascript
linux 后台运行node服务指令方法
May 23 Javascript
在vue中获取token,并将token写进header的方法
Sep 26 Javascript
element form 校验数组每一项实例代码
Oct 10 Javascript
微信小程序用户盒子、宫格列表的实现
Jul 01 Javascript
vue中data里面的数据相互使用方式
Jun 05 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用户名的密码加密更安全的方法
2019/06/21 PHP
Yii框架小部件(Widgets)用法实例详解
2020/05/15 PHP
10个实用的脚本代码工具
2010/05/04 Javascript
在Node.js中实现文件复制的方法和实例
2014/06/05 Javascript
jQuery实现表格展开与折叠的方法
2015/05/04 Javascript
微信js-sdk地理位置接口用法示例
2016/10/12 Javascript
Vue.js开发环境搭建
2016/11/10 Javascript
基于JQuery的Ajax方法使用详解
2017/08/16 jQuery
利用JS实现一个同Excel表现的智能填充算法
2018/08/13 Javascript
electron实现静默打印的示例代码
2019/08/12 Javascript
Vuex模块化应用实践示例
2020/02/03 Javascript
jquery轮播图插件使用方法详解
2020/07/31 jQuery
[01:11:35]Liquid vs LGD 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
Python 基于Twisted框架的文件夹网络传输源码
2016/08/28 Python
Django框架教程之正则表达式URL误区详解
2018/01/28 Python
Appium+python自动化之连接模拟器并启动淘宝APP(超详解)
2019/06/17 Python
pandas factorize实现将字符串特征转化为数字特征
2019/12/19 Python
手把手教你用纯css3实现轮播图效果实例
2017/05/04 HTML / CSS
Vans澳大利亚官网:购买鞋子、服装及配件
2019/09/05 全球购物
Ariat英国官网:为世界顶级马术运动员制造最优质的鞋类和服装
2020/02/14 全球购物
巴西网上药店:Drogaria Araujo
2021/01/06 全球购物
Prototype如何更新局部页面
2013/03/03 面试题
英文简历中的自我评价用语
2013/12/09 职场文书
校园餐饮创业计划书
2014/01/10 职场文书
七年级英语教学反思
2014/01/15 职场文书
颁奖晚会主持词
2014/03/25 职场文书
担保书怎么写
2014/04/01 职场文书
局火灾防控工作方案
2014/05/25 职场文书
观看信仰心得体会
2014/09/04 职场文书
2014年商场国庆节活动策划方案
2014/09/16 职场文书
公安机关正风肃纪剖析材料
2014/10/10 职场文书
团员自我评价范文
2015/03/10 职场文书
财务稽核岗位职责
2015/04/13 职场文书
小学作文之描写天气
2019/08/15 职场文书
Python基础数据类型tuple元组的概念与用法
2021/08/02 Python
Spark SQL 2.4.8 操作 Dataframe的两种方式
2021/10/16 SQL Server