web前端開(kāi)發(fā)也需要日志
更新時(shí)間:2010年12月09日 18:43:33 作者:
web前端開(kāi)發(fā)過(guò)程中調(diào)試是一個(gè)不可避免的過(guò)程,我們有眾多的瀏覽器可供選擇,但是如果您要調(diào)試的平臺(tái)瀏覽器不是那么先進(jìn)呢
如IE6,在他上面還不那么富饒,給程序調(diào)試帶來(lái)的很大的痛苦啊,所以日志即時(shí)輸出是一個(gè)不錯(cuò)的選擇,可以知道程序覆蓋到了哪里。即是在先進(jìn)的瀏覽器上也是很有必要的,他比去調(diào)試程序來(lái)確定相應(yīng)的業(yè)務(wù)代碼有沒(méi)有執(zhí)行到也快的多也容易的多,好了,下面來(lái)給大家介紹一個(gè)我業(yè)余時(shí)間寫的調(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('<div style="{0}"><table style="font-size:12px;border-collapse:collapse !important;"><tbody><tr><td style="height:25px;line-height:25px;border-right:3px solid #6CE26C;width:45px; text-align:center;"><code style="font-weight:bold;color:gray">{1}</code></td><td><b style="color:{2};margin-left:5px;">[{3}]:</b>{4}</td></tr></tbody></table></div>',
bgColor,this.__debugLine,"#333333",flag,msg):msg;
if(flag.constructor!=String)
msg = String.format('<div style="{0}"><table style="font-size:12px;;border-collapse:collapse !important;line-height:25px;"><tbody><tr><td style="height:25px;line-height:25px;border-right:3px solid #6CE26C;width:45px;text-align:center;"><code style="font-weight:bold;color:gray">{1}</code></td><td><b style="color:{2};margin-left:5px;">[{3}]:</b>{4}</td></tr></tbody></table></div>',
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 = "<span style='float:left;font-weight:bold;margin-left:10px;font-size:13px;'>調(diào)試信息控制臺(tái)</span><span id='console_andler' style='float:right;margin-right:20px;cursor:pointer' title='展開(kāi)/折疊'>+</span><br style='clear:both'>";
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 = ">>><input type = 'text' value='' id='console_eval' style='margin:0;width:90%;border:none;line-height:25px;height:25px;text-indent:10px;'/>";
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ù)暮?jiǎn)單
assert("debug","調(diào)試信息");
頁(yè)面中就會(huì)出相應(yīng)的日志。
該日志輸出部分代碼取自Jquery作者一個(gè)單元測(cè)試模塊。
上代碼:
復(fù)制代碼 代碼如下:
(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('<div style="{0}"><table style="font-size:12px;border-collapse:collapse !important;"><tbody><tr><td style="height:25px;line-height:25px;border-right:3px solid #6CE26C;width:45px; text-align:center;"><code style="font-weight:bold;color:gray">{1}</code></td><td><b style="color:{2};margin-left:5px;">[{3}]:</b>{4}</td></tr></tbody></table></div>',
bgColor,this.__debugLine,"#333333",flag,msg):msg;
if(flag.constructor!=String)
msg = String.format('<div style="{0}"><table style="font-size:12px;;border-collapse:collapse !important;line-height:25px;"><tbody><tr><td style="height:25px;line-height:25px;border-right:3px solid #6CE26C;width:45px;text-align:center;"><code style="font-weight:bold;color:gray">{1}</code></td><td><b style="color:{2};margin-left:5px;">[{3}]:</b>{4}</td></tr></tbody></table></div>',
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 = "<span style='float:left;font-weight:bold;margin-left:10px;font-size:13px;'>調(diào)試信息控制臺(tái)</span><span id='console_andler' style='float:right;margin-right:20px;cursor:pointer' title='展開(kāi)/折疊'>+</span><br style='clear:both'>";
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 = ">>><input type = 'text' value='' id='console_eval' style='margin:0;width:90%;border:none;line-height:25px;height:25px;text-indent:10px;'/>";
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ù)暮?jiǎn)單
復(fù)制代碼 代碼如下:
assert("debug","調(diào)試信息");
頁(yè)面中就會(huì)出相應(yīng)的日志。
該日志輸出部分代碼取自Jquery作者一個(gè)單元測(cè)試模塊。
您可能感興趣的文章:
- Web前端設(shè)計(jì)模式 制作漂亮的彈出層
- web的各種前端打印方法之jquery打印插件PrintArea實(shí)現(xiàn)網(wǎng)頁(yè)打印
- web的各種前端打印方法之jquery打印插件jqprint實(shí)現(xiàn)網(wǎng)頁(yè)打印
- ASP.net WebAPI 上傳圖片實(shí)例
- .Net基于MVC4 Web Api輸出Json格式實(shí)例
- web前端設(shè)計(jì)師們常用的jQuery特效插件匯總
- WEB前端設(shè)計(jì)師常用工具集錦
- 用Python編寫web API的教程
- 一張Web前端的思維導(dǎo)圖分享
- PhantomJS快速入門教程(服務(wù)器端的 JavaScript API 的 WebKit)
- web前端開(kāi)發(fā)JQuery常用實(shí)例代碼片段(50個(gè))
- 帶有定位當(dāng)前位置的百度地圖前端web api實(shí)例代碼
相關(guān)文章
JavaScript實(shí)現(xiàn)猜數(shù)字小功能
本文主要介紹了JavaScript實(shí)現(xiàn)猜數(shù)字小功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
javascript實(shí)現(xiàn)異形滾動(dòng)輪播
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)異形滾動(dòng)輪播,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
js實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
js與jquery獲取input輸入框中的值實(shí)例講解
在本篇文章里小編給大家整理了關(guān)于js與jquery獲取input輸入框中的值實(shí)例講解,需要的朋友們可以學(xué)習(xí)參考下。2020-02-02
JavaScript中Function函數(shù)與Object對(duì)象的關(guān)系
這篇文章主要介紹了JavaScript中Function函數(shù)與Object對(duì)象的關(guān)系的相關(guān)資料,需要的朋友可以參考下2015-12-12
IE8對(duì)JS通過(guò)屬性和數(shù)組遍歷解析不一樣的地方探討
如果是非IE8瀏覽器例如(IE7、IE9、Chrome、FF,僅測(cè)試這幾種)通過(guò)屬性和數(shù)組遍歷,其結(jié)果是一樣的,但對(duì)于IE8,結(jié)果會(huì)有一點(diǎn)小小的差異,在IE8下會(huì)把原型鏈擴(kuò)展方法當(dāng)做一個(gè)屬性輸出,大家可以測(cè)試一下2013-05-05

