为调试JavaScript添加输出窗口的代码

 更新时间:2010年02月07日 11:14:51   作者:  
调试JavaScript是一件很麻烦的事,尽管有很多很好用的调试工具,但有时候想要跟踪值的变化,但即不想中断脚本执行,也不想用alert显示值信息,这种情况下,一般的做法是在页面上添加一个DIV或者其它元素,然后再往里面添加调试信息。
虽然不是很复杂的实现,但每次都要这样就会很麻烦,所以我写了一小段脚本,用来自动生成这个输出窗口。
代码
复制代码 代码如下:

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);

调用之后,会自动在窗口的右下角出现一个固定位置的窗口,并显示相应的内容。效果图请看下面:

相关文章

  • JS+HTML+CSS实现轮播效果

    JS+HTML+CSS实现轮播效果

    这篇文章主要为大家详细介绍了JS+HTML+CSS实现轮播效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • 详解JavaScript中Hash Map映射结构的实现

    详解JavaScript中Hash Map映射结构的实现

    Hash Map通常在JavaScript中作为一个简单的来存储键值对的地方,不过哈希对象Object并不是一个真正的哈希映射,没Java中的Hash Map来的那么强大,well,接下来带大家详解JavaScript中Hash Map映射结构的实现
    2016-05-05
  • js中select选择器的change事件处理函数详解

    js中select选择器的change事件处理函数详解

    Js操作Select是很常见的,也是比较实用的,下面这篇文章主要给大家介绍了关于js中select选择器的change事件处理函数的相关资料,文中给出了详细的实例代码,需要的朋友可以参考下
    2023-06-06
  • JS实现的RGB网页颜色在线取色器完整实例

    JS实现的RGB网页颜色在线取色器完整实例

    这篇文章主要介绍了JS实现的RGB网页颜色在线取色器,结合完整实例形式分析了基于JS运算及鼠标事件响应来操作页面元素实现取色器功能的方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-12-12
  • 用js实现放大镜效果

    用js实现放大镜效果

    这篇文章主要为大家详细介绍了用js实现放大镜效果,利用背景图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • 微信小程序实现tab页面切换效果

    微信小程序实现tab页面切换效果

    这篇文章主要为大家详细介绍了微信小程序实现tab页面切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • 如何在CocosCreator中做一个List

    如何在CocosCreator中做一个List

    这篇文章主要介绍了如何在CocosCreator中做一个List,对List列表感兴趣的同学,不妨来试验一下
    2021-04-04
  • JavaScript降低代码圈复杂度优化技巧

    JavaScript降低代码圈复杂度优化技巧

    当一个项目经过持续迭代,不断增加功能,逐渐变成一个复杂的产品时,新功能的开发变得相对困难,其中一个很大的原因是代码复杂度高,导致可维护性和可读性都很差,本文将从前端JavaScript的角度出发,介绍一些有效的方法和技巧来优化前端代码的圈复杂度
    2023-10-10
  • 原生JS实现的轮播图功能详解

    原生JS实现的轮播图功能详解

    这篇文章主要介绍了原生JS实现的轮播图功能,结合实例形式分析了javascript实现轮播图的原理、操作技巧与相关注意事项,需要的朋友可以参考下
    2018-08-08
  • javascript 3d 逐侦产品展示(核心精简)

    javascript 3d 逐侦产品展示(核心精简)

    这篇文章主要介绍了javascript实现的3d逐侦产品展示,需要的朋友可以参考下
    2014-03-03

最新评论