Autor | Zpráva | ||
---|---|---|---|
Darker Profil |
#1 · Zasláno: 25. 7. 2010, 17:40:14 · Upravil/a: Darker
Nedávno jsem si hrál s js a nějak pomocí for(i in window) jsem vypsal názvy (ne hodnoty!) všech vlastností objektu window. A teď si nemůžu vzpomenout jak to bylo... Sestavil jsem akorát tohle a to je nanic...
for(i in window) document.write(window[i]) |
||
Nox Profil |
#2 · Zasláno: 25. 7. 2010, 17:47:07
dáváš do document.write celé window :)
document.write(i); |
||
Darker Profil |
#3 · Zasláno: 25. 7. 2010, 17:52:25 · Upravil/a: Darker
Takhle to funguje v PHP. Ale tam by to bylo
for($i as $window) echo $i; Chyba s i opravena, jen jsem se upsal když jsem to psal sem. Skript vrací hodnotz vlastností widow, neboli nevrátí barva ale "zelená" pokud je barva proměnná a "zelená" její hodnota. |
||
_es Profil |
#4 · Zasláno: 25. 7. 2010, 17:53:12
darker:
Tie vlastnosti asi neboli celkom všetky. Je v tom zozname napríklad objekt Math ?
|
||
_es Profil |
#5 · Zasláno: 25. 7. 2010, 18:10:21
darker:
Môžeš skúsiť napríklad tento JS odkaz: javascript:(function(){var s="<pre><xmp>"; for(var i in window)s+=i+": "+window[i]+"\n";var d=window.open("","_blank").document; d.write(s); d.close();})();void 0; |
||
Nox Profil |
#6 · Zasláno: 25. 7. 2010, 18:13:30 · Upravil/a: Nox
darker:
ale nesmysl - když pominu že v PHP je foreach, přečti si co napsal Chamurrapi "V iterační proměnné je u tohoto druhu cyklu vždy řetězec obsahující název členské proměnné" a to JE to co ty chceš a chybí ti tam var |
||
Darker Profil |
#7 · Zasláno: 25. 7. 2010, 18:15:24
Aha, promiň, měls pravdu. Vážně se má vypisovat i.
Tak dík. |
||
Witiko Profil |
#8 · Zasláno: 25. 7. 2010, 23:18:27
Před časem jsem napsal v js třídu collect, která se starala o výpis všech objektů a podobjektů daného objektu. Byla zahrnutá i ochrana proti zacyklení. (window.self.self.self.self.self...) Můžu to sem pro zajímavost hodit.
|
||
Witiko Profil |
#9 · Zasláno: 26. 7. 2010, 19:06:56
if(!Array.indexOf){ Array.prototype.indexOf = function(obj){ for(var i=0; i<this.length; i++){ if(this[i]==obj){ return i; } } return -1; } } var Collector = function() { var tree = {}; var duplicitytree = {}; var errorstree = []; var exceptions = ["_firebug","innerHTML","outerHTML","innerText","outerText","style","location","textContent","name","wholeText","mimeTypes"]; var interval = 100; var engaged = false; var basictree = []; var that = this; this.run = function(object, objectname, progresscallback, callback, step) { if(typeof step == "undefined") { if(!engaged) { engaged = true; cleanup(); for(var subobject in object) { basictree.push(subobject); } this.run(object, objectname, progresscallback, callback, 0); } } else { try{ var original_step = step; if(step == basictree.length) { engaged = false; if(callback && typeof callback == "function") callback(tree, errorstree); } else { step++; if(exceptions.indexOf(basictree[step-1]) == -1) { if(typeof object[basictree[step-1]] == "object") timeout(function(){collect(object, [objectname, basictree[step-1]], function(){progresscallback(step / basictree.length);that.run(object, objectname, progresscallback, callback, step)})}); else { if(typeof object[basictree[step-1]] != "function") duplicitytree[objectname + "." + basictree[step-1]] = tree[(function(){return objectname.length?objectname + ".":"";})() + basictree[step-1]] = object[basictree[step-1]]; progresscallback(step / basictree.length); timeout(function(){ that.run(object, objectname, progresscallback, callback, step); }); } } else { progresscallback(step / basictree.length); this.run(object, objectname, progresscallback, callback, step) } } }catch(e){ errorstree.push(e); if(original_step == step) step++; progresscallback(step / basictree.length); this.run(object, objectname, progresscallback, callback, step) } } } var cleanup = function() { basictree = []; tree = {}; duplicitytree = {}; errorstree = []; } var collect = function(object, array, callback) { try{ var array_; var link = getlink(object, array.slice(1)); var name = getname(array); for (var subobject in link) { if(duplicitytest(link[subobject]) && exceptions.indexOf(subobject) == -1) { duplicitytree[name + "." + subobject] = link[subobject]; if(typeof link[subobject] != "object" && typeof link[subobject] != "function") {tree[name + "." + subobject] = link[subobject];} else if(typeof link[subobject] == "object") {array_ = array.slice(0); array_.push(subobject); collect(object, array_);} } } }catch(e){ errorstree.push(e); } if(callback && typeof callback == "function") callback(); } var timeout = function(action) { window.setTimeout(action, interval); } var getlink = function(object, array) { if(array.length > 1) return getlink(object[array[0]], array.slice(1)); else return object[array[0]]; } var getname = function(array) { name = array[0]; for(var count = 1; count < array.length;) { if(array[count] == array[count-1]) array = Array.concat(array.slice(0, count), array.slice(count+1)) else count++; } for(var count = 1; count < array.length; count++) { name += (function(){return name.length?".":"";})() + array[count]; } return name; } var duplicitytest = function(object) { for(var subobject in duplicitytree) { if(object == duplicitytree[subobject]) return false; } return true; } } Zavolá se to takto: var collector = new Collector(); collector.run(window, "window", function(progress) { status = "Hotovo "+String(Math.round(progress*100)) + "%"; }, function(tree,errors) { var text, win = window.open(); for(var i in tree) { text += "<b>" + i + "</b> => " + tree[i] + "<br>"; } win.document.write(tree); }); Uznávám, že generování jména, testování duplicitního objektu apod. šlo možná vyřešit s větší elegancí a občas by to mohlo být méně splácané, ale vypíše to veškeré podobjekty bez zacyklení se. |
||
Časová prodleva: 14 let
|
0