为调试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 相关文章推荐
JSQL 基于客户端的成绩统计实现方法
May 05 Javascript
30个让人兴奋的视差滚动(Parallax Scrolling)效果网站
Mar 04 Javascript
Jquery模仿Baidu、Google搜索时自动补充搜索结果提示
Dec 26 Javascript
js清理Word格式示例代码
Feb 13 Javascript
js实现从数组里随机获取元素
Jan 12 Javascript
微信小程序前端源码逻辑和工作流
Sep 25 Javascript
js document.getElementsByClassName的使用介绍与自定义函数
Nov 25 Javascript
浅析bootstrap原理及优缺点
Mar 19 Javascript
使用vue-cli3 创建vue项目并配置VS Code 自动代码格式化 vue语法高亮问题
May 14 Javascript
原生js实现贪食蛇小游戏的思路详解
Nov 26 Javascript
jQuery实现点击滚动到指定元素上的方法分析
Mar 19 jQuery
JS跨浏览器解析XML应用过程详解
Oct 16 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
phpadmin如何导入导出大数据文件及php.ini参数修改
2013/02/18 PHP
探讨php中header的用法详解
2013/06/07 PHP
PHP基于swoole多进程操作示例
2019/08/12 PHP
URL地址中的#符号使用说明
2011/02/12 Javascript
js Event对象的5种坐标
2011/09/12 Javascript
计算新浪Weibo消息长度(还可以输入119字)
2013/07/02 Javascript
使用jquery解析XML示例代码
2014/09/05 Javascript
实例分析javascript中的call()和apply()方法
2014/11/28 Javascript
BootStrap下jQuery自动完成的样式调整
2016/05/30 Javascript
JavaScript知识点总结(十)之this关键字
2016/05/31 Javascript
echarts3 使用总结(绘制各种图表,地图)
2017/01/05 Javascript
vue2.0 自定义日期时间过滤器
2017/06/07 Javascript
js脚本编写简单刷票投票系统
2017/06/27 Javascript
jquery.picsign图片标注组件实例详解
2018/02/02 jQuery
浅谈vue 单文件探索
2018/09/05 Javascript
vue src动态加载请求获取图片的方法
2018/10/17 Javascript
Python发送Email方法实例
2014/08/21 Python
详解Python文本操作相关模块
2017/06/22 Python
Python hashlib模块用法实例分析
2018/06/12 Python
python GUI库图形界面开发之PyQt5控件QTableWidget详细使用方法与属性
2020/02/25 Python
如何用Python徒手写线性回归
2021/01/25 Python
CSS3 @font-face属性使用指南
2014/12/12 HTML / CSS
CSS Grid布局教程之网格单元格布局
2014/12/30 HTML / CSS
澳大利亚领先的在线美容商店:Facial Co
2017/10/22 全球购物
既然说Ruby中一切都是对象,那么Ruby中类也是对象吗
2013/01/26 面试题
《掌声》教学反思
2014/02/23 职场文书
爱情保证书大全
2014/04/29 职场文书
记账会计岗位职责
2014/06/16 职场文书
英语自我介绍演讲稿
2014/09/01 职场文书
大学生考试作弊检讨书
2014/09/21 职场文书
民主生活会批评与自我批评总结
2014/10/17 职场文书
2015秋季新学期开学寄语
2015/05/28 职场文书
2015个人年度工作总结范文
2015/05/28 职场文书
详解CocosCreator项目结构机制
2021/04/14 Javascript
springboot入门 之profile设置方式
2022/04/04 Java/Android
JavaScript原型链中函数和对象的理解
2022/06/16 Javascript