The function that writes a print information is more powerful than alert, similar to the console. Table in firebug.

Source: Internet
Author: User

sometimes, especially when an Ajax request returns data, we need to know the structure of the returned data. If it is an object, alert cannot be printed. You can view it on the console in firebug, I wrote a function myself, a bit like this. Sometimes I think it is not intuitive to use the console, so I want to write a function. However, the level supported now is three layers, and it won't work if I have more layers, an object will appear when there is more.

If (! Window. debug) {window. debug ={}; // select your own name and never conflict with others}; debug.info = function (OBJ) {If (typeof _ debugstatus _! = "Undefined") {return;}; var version = "1.0", STR = "", type, olen = 0, ML = 0, substr = "", oimstr = "", region R = ""; var ST = document. createelement ("style"); ST. innerhtml = ''+ '. layer {font-family: Arial; font-size: 14px; color: #333; line-Height: 150%; width: 100%; Height: 100%; Background: rgba, 0, 0.5); position: absolute; left: 0; top: 0; Z-index: 9999} '+ '. debug-box {width: 900px; position: relative; left: 50%; top: 50%; margin-left:-pixel PX; margin-top:-250px; Background: white; box-Shadow: 0 0 50px rgba (0.5, 0,);} '+ '. debug-title {Line-Height: 35px; padding: 0 10px; border-bottom: 1px solid # ae0; Background: # ded} '+ '. debug-names {width: 20%; float: Left; display: inline-block; * display: inline; * ZOOM: 1; color: green; text-indent: 5px} '+ '. debug-values {width: 70%; float: Right; color: Orange} '+ '. debug-close {Line-Height: 35px; position: absolute; Right: 0; top: 0; color: #666; padding: 0 10px; cursor: pointer} '+ '. ULS {padding: 0; margin: 30px} '+ '. debug-clear {clear: Both; line-Height: 0; Height: 0;} '+ '. debug-line {width: 5%; float: Left; color: Blue} '+ '. debug-CS {overflow-Y: auto; Height: 500px; padding-bottom: 10px} '+' # debuglayer Li {border-bottom: 1px solid # Eee; List-style: none; line-Height: 30px;} '+' # debuglayer Li: hover {Background: # edf4ed} '+ '. debug-obj-Info {color: #666;} '+ '. debug-OBJ {font-weight: bold; color: green; margin: 10px} '; var H = document. getelementsbytagname ("head") [0]; H. appendchild (ST); If (typeof OBJ = "string") {STR = OBJ; type = "string";} else if (typeof OBJ = "object ") {If (OBJ instanceof array) {for (VAR I = 0, Len = obj. length; I <Len; I ++) {STR + = '<li> <Div class = "debug-names">' + I + '</div> <Div class = "debug-line" >=> </div> <Div class = "debug-values"> '+ OBJ [I] +' </div> <Div class = "debug-clear"> </div> </LI> ';} type = "array"; olen = Len;} else {for (var I in OBJ) {If (typeof OBJ [I] = "object ") {If (OBJ [I] instanceof array) {for (VAR m = 0; m <OBJ [I]. length; m ++) {If (typeof OBJ [I] = "object") {If (OBJ [I] instanceof array) {VaR _ A = ""; for (var k = 0; k <OBJ [I]. length; k ++) {_ A + = '<Div class = "debug-names">' + K + '</div> <Div class = "debug-line" >=> </Div> <Div class = "debug-values"> '+ OBJ [I] [k] +' </div> <Div class = "debug-clear"> </div> ';} else R + = '<Div class = "debug-names">' + M + '</div> <Div class = "debug-line" >=> </div> <div class = "debug-values"> '+ _ A +' </div> <Div class = "debug-clear"> </div> ';} else {VaR _ B = ""; for (var n in OBJ [I]) {_ B + = '<Div class = "debug-names">' + N + '</div> <Div class = "debug-line" >=> </Div> <Div class = "debug-values"> '+ OBJ [I] [N] +' </div> <Div class = "debug-clear"> </div> ';} else R + = '<Div class = "debug-names">' + M + '</div> <Div class = "debug-line" >=> </div> <div class = "debug-values"> '+ _ B +' </div> ';}} else {release R + = '<Div class = "debug-names">' + M + '</div> <Div class = "debug-line" >=> </Div> <Div class = "debug-values"> '+ OBJ [I] [m] +' </div> <Div class = "debug-clear"> </div> ';}} substr + = reverse R; STR + = '<li> <Div class = "debug-names">' + I + '</div> <Div class = "debug-line" >=> </ div> <Div class = "debug-values"> '+ substr +' </div> <Div class = "debug-clear"> </div> </LI>' ;} else {for (VAR m in OBJ [I]) {If (typeof OBJ [I] [m] = "object ") {If (OBJ [I] [m] instanceof array) {for (var j = 0; j <OBJ [I] [M]. length; j ++) {oimstr + = '<Div class = "debug-names">' + J + '</div>' + '<Div class = "debug-line" >=> </div> '+' <Div class = "debug-values"> '+ OBJ [I] [m] [J] +' </div> <Div class = "Debug -Clear "> </div> ';} substr = oimstr;} else {for (var j in OBJ [I] [m]) {oimstr + = '<Div class = "debug-names">' + J + '</div>' + '<Div class = "debug-line" >=> </div> '+' <Div class = "debug-values"> '+ OBJ [I] [m] [J] +' </div> <Div class = "Debug -Clear "> </div> ';} substr = oimstr ;}} else {substr + = '<Div class = "debug-names">' + M + '</div>' + '<Div class = "debug-line" >=> </div> '+' <Div class = "debug-values"> '+ OBJ [I] [m] +' </div> <Div class = "debug-clear "></div> ';}} STR + = '<li> <Div class = "debug-names">' + I + '</div> <Div class = "debug-line" >=> </ div> <Div class = "debug-values"> '+ substr +' </div> <Div class = "debug-clear"> </div> </LI>' ;}} else {STR + = '<li> <Div class = "debug-names">' + I + '</div> <Div class = "debug-line" >=> </div> <Div class = "debug-values"> '+ OBJ [I] +' </div> <Div class = "debug-clear"> </div> </LI> ';} ML ++;} type = "object"; olen = ML ;}} else if (OBJ instanceof function) {STR = OBJ; type = "function ";} result = '<ul class = "ULS"> <li class = "debug-obj-Info"> This Is A <SPAN class = "debug-OBJ">' + Type + '</span> info lenght: '+ olen +' </LI> '+ STR +' </ul> '; var layer = document. createelement ("Div"); var box = document. createelement ("Div"); var Title = document. createelement ("Div"); var content = document. createelement ("Div"); layer. setattribute ("class", "layer"); layer. setattribute ("ID", "debuglayer"); box. setattribute ("class", "debug-box"); box. setattribute ("ID", "debugbox"); title. setattribute ("ID", "debugtitle"); title. setattribute ("class", "debug-title"); layer. appendchild (box); content. setattribute ("ID", "debugcontent"); content. setattribute ("class", "debug-CS"); box. appendchild (title); box. appendchild (content); title. innerhtml = 'debug tool' + version + '<span id = "debugclose" class = "debug-Close" Title = "Close debug"> close </span>'; content. innerhtml = result; document. body. appendchild (layer); document. getelementbyid ("debugclose "). onclick = function () {document. body. removechild (layer);} layer. onclick = function (e) {If (document. all) {window. event. cancelbubble = true;} else {e. stoppropagation () ;}} return _ debugstatus _ = 0 ;}

 

Add a layer in the body and output the information. This layer is intuitive and easy to use.

 
// Test the effect of debug.info (Navigator); // Hey, is it cool?

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.