简单的js调试函数

有时候我们不得不把某个js的数组或者对象打印出来,就好像php的print_r一样,js没有这种函数,我在网上找了一个并且稍微改造了一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//js调试函数
function dump(o){
	tips=document.createElement("div");
	tips.className='dump_tips';
	tips.style.cssText="position:fixed;bottom:0;left:10px;height:400px;width:400px;overflow:auto;background:#000;border:1px solid #333;padding:10px";
	tips.innerHTML=(print_r(o));
	document.getElementsByTagName('BODY')[0].appendChild(tips);
	print_r(o);
	function print_r(o){
		function print(o,i){
			i = i || 0;  // how many tabs
			var color = '#0074ce'; 
			// randomly generate a color
			var indent = '<font style="color:#333">-----</font>'
			// form of a tab, you can use '|\t' instead
 
			var space = '';
			for( var k=0; k<i; k++ ){
				space += indent;
			}
			var ret = '';
			for( var name in o ){
				var val = o[name];   
				ret += space + '<font style="color:#96eff2">' + name + ' <font style="color:#6d6d6d">=></font> '+ '<font style="color:#666">' + typeof(val) +'</font>'+'</font>'+'<font style="color:#6d6d6d">( </font>';
				ret+='<font style="color:#96eff2">';
				if( typeof val == 'object' ){
					ret += '\n'+print(val,i+1);
					ret += space;
				} else if( typeof val == 'function' ){
					ret += '\n' + space + indent + val.toString().replace(/\n/g, '\n'+space+indent)+'\n'+space;
				} else
					ret += val + ' ' ;
				ret+='</font>';
				ret += '<font style="color:#6d6d6d">' + ')</font>\n';
			}
			return ret;
		}
		if( typeof o == 'object' )
			return '《pre》'+print(o)+'《/pre》';//把这里《》变成<>
		else
			return o;
	}
}

6 Responses to 简单的js调试函数 »

  1. allenm 评论 2010-10-20 09:28

    用调试工具的 console.dir(array) 就好了,很方便

    回复

    joe 回复 十月 20th, 2010 at 17:52

    ie里面也可以用?

    回复

    allenm 回复 十月 20th, 2010 at 22:50

    IE 里面可以使用 firebug lite

    回复

    joe 回复 十月 21st, 2010 at 14:56

    好的哈~谢谢伦哥

  2. 果果 评论 2010-11-07 10:11

    这个简单,呵呵

    回复

  3. 简单的js调试函数 Pingback 2011-05-24 10:27

    [...] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 //js调试函数 function dump(o){ tips=document.createElement("div"); tips.className=’dump_tips’; tips.style.cssText="position:fixed;bottom:0;left:10px;height:400px;width:400px;overflow:auto;background:#000;border:1px solid #333;padding:10px"; tips.innerHTML=(print_r(o)); document.getElementsByTagName(’BODY’)[0].appendChild(tips); print_r(o); function print_r(o){ function print(o,i){ i = i || 0; // how many tabs var color = ‘#0074ce’; // randomly generate a color var indent = ‘<font style="color:#333">—–</font>’ // form of a tab, you can use ‘|t’ instead   var space = ”; for( var k=0; k<i; k++ ){ space += indent; } var ret = ”; for( var name in o ){ var val [...] View full post on 写笔记的小JOE页 [...]

Leave a Reply

Email address is not published

You should say a Chinese word to pass spam check. If you can not input Chinese, just copy 你好 and paste them into comment text box.