為調(diào)試JavaScript添加輸出窗口的代碼
代碼
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 = "關(guān)閉";
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 = "";
}
調(diào)用方法很簡(jiǎn)單:
Babu.Debugging.writeLine("調(diào)試信息");
Babu.Debugging.writeLine("帶參數(shù)的調(diào)試信息:參數(shù)1={0},參數(shù)2={1}", arg1, arg2);
調(diào)用之后,會(huì)自動(dòng)在窗口的右下角出現(xiàn)一個(gè)固定位置的窗口,并顯示相應(yīng)的內(nèi)容。效果圖請(qǐng)看下面:
- javascript代碼調(diào)試之console.log 用法圖文詳解
- js調(diào)試工具console.log()方法查看js代碼的執(zhí)行情況
- 調(diào)試Javascript代碼(瀏覽器F12及VS中debugger關(guān)鍵字)
- 在vs2010中調(diào)試javascript代碼方法
- 分享一個(gè)自定義的console類 讓你不再糾結(jié)JS中的調(diào)試代碼的兼容
- 利用vscode調(diào)試編譯后的js代碼詳解
- 簡(jiǎn)單實(shí)用的js調(diào)試logger組件實(shí)現(xiàn)代碼
- JavaScript代碼調(diào)試方法實(shí)例小結(jié)
相關(guān)文章
Javascript實(shí)現(xiàn)簡(jiǎn)單的富文本編輯器附演示
這篇文章主要介紹了通過Javascript實(shí)現(xiàn)的簡(jiǎn)單富文本編輯器,需要的朋友可以參考下2014-06-06
小程序封裝wx.request請(qǐng)求并創(chuàng)建接口管理文件的實(shí)現(xiàn)
這篇文章主要介紹了小程序封裝wx.request請(qǐng)求并創(chuàng)建接口管理文件2019-04-04
javascript結(jié)合Canvas 實(shí)現(xiàn)簡(jiǎn)易的圓形時(shí)鐘
本文給大家分享的是javascript結(jié)合Canvas 實(shí)現(xiàn)簡(jiǎn)易的圓形時(shí)鐘,主要是對(duì)自己前段時(shí)間學(xué)習(xí)html5的canvas的一次小檢驗(yàn),這里推薦給小伙伴們,有需要的可以參考下。2015-03-03
javascript設(shè)計(jì)模式 – 組合模式原理與應(yīng)用實(shí)例分析
這篇文章主要介紹了javascript設(shè)計(jì)模式 – 組合模式,結(jié)合實(shí)例形式分析了javascript組合模式相關(guān)概念、原理、應(yīng)用場(chǎng)景及相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2020-04-04
原生微信小程序中封裝一個(gè)模擬select下拉框組件代碼示例
這篇文章主要給大家介紹了關(guān)于原生微信小程序中封裝一個(gè)模擬select下拉框組件的相關(guān)資料,文中介紹了如何在小程序中創(chuàng)建和使用自定義組件van-select,包括組件的創(chuàng)建步驟和在頁面中的應(yīng)用方法,需要的朋友可以參考下2024-11-11
JavaScript使用HTML5的window.postMessage實(shí)現(xiàn)跨域通信例子
這篇文章主要介紹了JavaScript使用HTML5的window.postMessage實(shí)現(xiàn)跨域通信例子,需要的朋友可以參考下2014-04-04
ES6知識(shí)點(diǎn)整理之函數(shù)數(shù)組參數(shù)的默認(rèn)值及其解構(gòu)應(yīng)用示例
這篇文章主要介紹了ES6知識(shí)點(diǎn)整理之函數(shù)數(shù)組參數(shù)的默認(rèn)值及其解構(gòu)應(yīng)用,結(jié)合實(shí)例形式分析了ES6函數(shù)數(shù)組參數(shù)解構(gòu)賦值和默認(rèn)值的設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2019-04-04

