Web :: General :: How To dump a javascript variable to check its contents (like var_dump() or print_r() ) |
|||
| By: VGR |
Date: 30/03/2007 22:44:40 |
Points: 0 | Status: Answered Quality : Excellent |
|
here's some code. You've to customize the attributes you don't want and those you want. Have fun. Translating the features explained in the comments : - allows to see attributes of any object in an alert() box that will fit on a display in 1024x768 minimum - aggregates object attributes to save space - doesn't show event handlers (on*) - controls length of attributes innerHTML, outerHTML, innerText, outerText so that they fit the window. //VGR23052006 MOD nouvelle variante de dump_var, plus intelligente et pratique pour déboguer en IExploder (car sans Web Developer ;-) // REM particularités : doit permettre de "voir" les attributs de n'importe quel objet dans une alert() tenant sur un écran en 1024x768 minimum // condense les attributs "object" pour gagner de la place // ne montre pas les attributs on* (event handlers) // contrôle de longueur des attributs innerHTML, outerHTML, innerText, outerText Array.prototype.in_array = function (value) { //VGR REM adapted from http://code.mikebrittain.com/?p=8 // Returns true if the passed value is found in the // array. Returns false if it is not. var i; for (i=0; i < this.length; i++) { // Matches identical (===), not just similar (==). if (this === value) return true; } return false; }; // in_array var eoln='\n'; var badAttribs = new Array("language","lang","recordNumber","scopeName","runtimeStyle","filters","tagUrn","behaviorUrns","all", "protocol","urn","port","rev","search","hash","dataFld","port","hreflang","shape","type","dataFormatAs"); var globMaxLength=256; // limite d'affichage des contenus var limitlength=new Array("innerHTML", "outerHTML", "innerText", "outerText"); // good attribs constrained by globMaxLength above function dump_var(elm) { var str = ""; var dumpobj = ""; var theattrib, strlen; for (var i in elm){ if (i.substring(0,2)!='on') { // no handlers if (!badAttribs.in_array(i)) { theattrib=elm.getAttribute(i); if (typeof theattrib == "object") dumpobj += i + " ; "; else { if (limitlength.in_array(i)) { // contrôle de longueur strlen=theattrib.length; if (strlen>globMaxLength) theattrib=theattrib.substring(0,globMaxLength)+" [...]"; } // else NOP str += i + " : " + theattrib + eoln; } // if objet ou non } // else NOP } // else NOP } // for attributes alert(str+"objects : "+dumpobj); } //EoMod |
|||
| By: VGR | Date: 30/03/2007 23:18:11 | Type : Comment |
|
| The very basic version was : //VGR15102003 FIXed bugs hors IE function inspect(elm){ var str = ""; for (var i in elm){ str += i + ": " + elm.getAttribute(i) + "\\n"; } alert(str); } NB : fix the double antislash depending on the way you output this to your DHTML page. It's probably better to use an "$eoln" variable. |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








