visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: DHTML, JScript :: Knowledge Base : dump_var function in Javascript ; alert with attributes value of object


By: VGR France  Date: 23/05/2006 11:06:16  English French  Points: 0 Status: Answered
Quality : Excellent
This is a small function I had under my ankle for quite a while and that I modified today. It may help to save time for some people around.

It enables to display the attributes of any object. It's no sorcery but it's handy. What I did today is modify it so that it displays fine on "small" screen resolution (the more-than-common 1024x768) and prevents too long contents (like innerHTML...) and [object] values (not interesting anyway, this is not a recursive function ; call it with the sub-object if you want ;-) to produce too long a result, especially for an alert() box you can't control the height of.
I also chose to not display the values of the event handler attributes, and also not some attributes I found particularly IE-proprietary or uninteresting ("filters", "tagUrn", "host", "protocol"=HTTP...). You may adapt to your needs le cas échéant.

The only case when this function may produce too high an alert box is when when you defined additional "function attributes" for that object. Their body had better to be short ;-)

here's an example :


and this is the source code :

//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



Bon amusement et j'espère que ça vous rendra service autant qu'à moi, surtout avec Internet Exploder qui n'a pas d'extension Web Developer ;-)

Do register to be able to answer

EContact
browser fav
page generated in 287.863970 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page