
如IE6,在他上面還不那么富饒,給程序調(diào)試帶來的很大的痛苦啊,所以日志即時輸出是一個不錯的選擇,可以知道程序覆蓋到了哪里。即是在先進(jìn)的瀏覽器上也是很有必要的,他比去調(diào)試程序來確定相應(yīng)的業(yè)務(wù)代碼有沒有執(zhí)行到也快的多也容易的多,好了,下面來給大家介紹一個我業(yè)余時間寫的調(diào)試信息輸出工具。
上代碼:
代碼如下:
(function(){
var cache = [];
var el = null;
this.__debugLine = 1;
function parseObjToStr(obj){
if(obj.constructor == String){
return obj.toString();
}
var ret = [];
for(var o in obj){
if(typeof obj[o]!="function")
ret.push(o+":"+obj[o]);
}
return ret.join(",");
}
this.assert = function(flag,msg){
msg = {"number":1,"string":1,"boolean":1,"function":1,"undefined":1}[typeof msg]?msg:parseObjToStr(msg);
//Log.getInstance().debug(msg);
return;
var bgColor = this.__debugLine%2==0?"background-color:#F8F8F8":"background-color:#ffffff";
msg = flag=="debug"?String.format('
',
bgColor,this.__debugLine,"#333333",flag,msg):msg;
if(flag.constructor!=String)
msg = String.format('
',
bgColor,this.__debugLine,flag?"green":"red",flag?"PASS ":"FAIL ",msg);
this.__debugLine++;
if(cache!=null){
cache[cache.length] = msg;
}
else{
el.innerHTML += msg;
}
}
function applyStyle(el,style){
for(var pro in style){
el.style[pro] = style[pro];
}
}
addEvent(window,"load",function(){
return;
el = document.createElement("div");
var elStyle ={backgroundColor:"#ffffff",color:"#333333",border:"1px solid #dcdada",borderLeft:"0px solid #6CE26C",borderRight:"0px solid #6CE26C"
,lineHeight :"25px",textAlign:"left",listStyleType :"none",margin:"0px",maxHeight:"200px",overflow:"auto"};
var head = document.createElement("div");
var headStyle ={backgroundColor:"#fef5c5",lineHeight:"25px"};
head.innerHTML = "調(diào)試信息控制臺+
";
var wrap = document.createElement("div");
var wrapStyle ={overflow:"hidden",backgroundColor:"#ffffff",color:"#333333",border:"1px solid #C0C0C0","fontSize":"12px","margin":"5px",position:"fixed",left:"0px",bottom:"0px",width:"97%"};
var foot = document.createElement("div");
var footStyle ={padding:"0",textAlign:"left"};
foot.innerHTML = ">>>
";
applyStyle(wrap,wrapStyle);
applyStyle(head,headStyle);
applyStyle(el,elStyle);
applyStyle(foot,footStyle);
wrap.appendChild(head);
wrap.appendChild(el);
wrap.appendChild(foot);
document.body.appendChild(wrap);
el.innerHTML = cache.join("");
cache = null;
function toggle(){
if(!this.hide){
el.style.display = "none";
foot.style.display = "none";
wrap.style.width = "200px";
this.hide = true;
}
else{
el.style.display = "";
foot.style.display = "";
wrap.style.width = "98%";
this.hide = false;
}
}
head.onclick = function(){
toggle.call(this);
}
head.onclick();
document.getElementById("console_eval").onkeydown = function(e){
e = e||window.event;
if(e.keyCode==13){
try{
eval.call(window,String.format("assert('debug',{0})",this.value));
}
catch(e){
assert("debug",e.message);
}
el.scrollTop = el.scrollHeight;
}
}
});
})();
上面代碼調(diào)用也相當(dāng)?shù)暮唵?
代碼如下:
assert("debug","調(diào)試信息");
頁面中就會出相應(yīng)的日志。
該日志輸出部分代碼取自Jquery作者一個單元測試模塊。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com