Autor | Zpráva | ||
---|---|---|---|
John58 Profil * |
#1 · Zasláno: 26. 9. 2011, 23:03:46
Ahoj, tak ješte jedna větší prosba. Toto je javascript me stránky a potřebuji ho zkontrolovat. Ne přímo výpočty atd, to ani bez náhledu nejde ale jen takové ty věci, co by šlo udělat lépe. Jestli použivat nějakou třídu protože v tom ješte nejsem tak zběhly, atd. Diky za pomoc.
var regurl = /(http|https):\/\/([a-zA-Z0-9\.\-]+)/g; // Zkraceni URL adresy var inter; onload = function(){ var page = document.getElementById('page'); var main = document.getElementById('maindown'); var profil = false; if(acc.login&&con){ var pf={bpwin:{bt:document.getElementById('bpwin'),sta:false},msgwin:{bt:document.getElementById('bmsgwin'),sta:false}}; } page.load('result.php?con='+con+'&action=mainpage'); getClass('bt btex btRa').Ar(function(el){ el.onmouseover = function(){ this.style.background = "url('img/opacity.png')"; }; el.onmouseout = function(){ this.style.background = "none"; }; }); getClass('bt').Ar(function(el){ el.onclick = function(){ window.location.hash=this.getAttribute('href'); page.load('result.php?action='+this.getAttribute('href')+'&con='+con,function(){ getClass('btM').Ar(function(el){ el.onclick = function(){ window.location.hash=this.getAttribute('href'); page.load('result.php?action='+this.getAttribute("href")+'&con='+con,function(){ getClass('free')[0].mouse(function(){ this.style.background = '#f1f1f1'; },function(){ this.style.background = '#f9f9f9'; }); nf = getClass('nofreesp')[0]; nf.mouse(function(){ this.style.background = '#f1f1f1'; },function(){ this.style.background = '#f9f9f9'; }); nf.onclick = function(){ if(!acc.login){ location.href="acc"; } }; }); return false; }; }); }); return false; }; }); document.getElementById('logo').onclick = function(){ window.location.hash=this.getAttribute('href'); page.load('result.php?con='+con+'&action=mainpage'); return false; } if(acc.login){ pf.bpwin.bt.onclick = function(){ if(pf.bpwin.sta){ pf.bpwin.sta = false; main.removeChild(pf.bpwin.el); return false; } else pf.bpwin.sta = true; pf.bpwin.el = document.createElement('div'); pf.bpwin.el.setAttribute('id','pwin'); pf.bpwin.el.className += ' hackbox '; pf.bpwin.el.load('result.php?con='+con+'&action=profilwindow',function(){ pf.bpwin.el.style.left = (pf.bpwin.bt.offsetLeft-270+pf.bpwin.bt.offsetWidth)+'px'; pf.bpwin.img = getClass('pImg',pf.bpwin.el)[0]; pf.bpwin.img.onload = function(){ getClass('pImgload',pf.bpwin.el)[0].style.display = 'none'; pf.bpwin.img.style.display = 'inline'; }; main.appendChild(pf.bpwin.el); }); return false; }; } if(acc.login){ pf.msgwin.bt.onclick = function(){ msgwin(); }; msgwin = function(){ if(pf.msgwin.sta){ pf.msgwin.sta = false; main.removeChild(pf.msgwin.el); page.style.width = ''; document.cookie='msgwin=false'; return false; } else pf.msgwin.sta = true; document.cookie='msgwin=true'; if(pf.bpwin.sta) main.removeChild(pf.bpwin.el); pf.bpwin.sta=false; page.style.width = ((document.documentElement.clientWidth-290)>600)?(document.documentElement.clientWidth-290)+'px':'600px'; pf.msgwin.el = document.createElement('div'); pf.msgwin.el.setAttribute('id','msgwin'); pf.msgwin.el.style.height = document.documentElement.clientHeight-50+'px'; pf.msgwin.bt.style.background = "#f1f1f1"; pf.msgwin.el.load('result.php?con='+con+'&action=msg',function(){ main.appendChild(pf.msgwin.el); getClass('msgOne').Ar(function(el){ el.mouse(function(){ this.style.background = '#dddddd'; },function(){ this.style.background = '#f9f9f9'; }); }); pf.msgwin.el.mousewheel(function(e){ win = document.getElementById('msgAll'); if(e<0&&parseInt(win.style.top)>=15){ //UP win.style.top = (parseInt(win.style.top)-15)+"px"; } else if(e>0&&(parseInt(win.style.top)+parseInt(win.offsetHeight)+20)<parseInt(pf.msgwin.el.style.height)){ //DOWN win.style.top = (parseInt(win.style.top)+15)+"px"; inter = window.setInterval("backTop()", 40); } }); window.onresize = function(){ if(pf.msgwin.sta){ page.style.width = ((document.documentElement.clientWidth-290)>600)?(document.documentElement.clientWidth-290)+'px':'600px'; } } }); return false; } if((rCook('msgwin')=="true")&&(document.documentElement.clientWidth>700))msgwin(); } // IS login }; window.onresize = function(event) { if(pf.bpwin.sta){ pf.bpwin.el.style.left = (pf.bpwin.bt.offsetLeft-pf.bpwin.el.offsetWidth+pf.bpwin.bt.offsetWidth)+'px'; pf.msgwin.el.style.height = document.documentElement.clientHeight-50+'px'; } } backTop = function(win){ i=parseInt(document.getElementById('msgAll').style.top)-1; if(i>0) { document.getElementById('msgAll').style.top = i+'px'; } } function rCook(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } Element.prototype.mousewheel = function(f){ var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel"; if (this.attachEvent) //if IE (and Opera depending on user setting) this.attachEvent("on"+mousewheelevt, function(e){f.call(this,e.detail? e.detail : e.wheelDelta)}) else if (this.addEventListener) //WC3 browsers this.addEventListener(mousewheelevt, function(e){f.call(this,e.detail? e.detail : e.wheelDelta)}, false) } Element.prototype.load = function(url,cb){ var el=this; var httpRequest; if(url!=0){ if(window.ActiveXObject){ httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } else { httpRequest = new XMLHttpRequest(); } httpRequest.open('Get',url,true); httpRequest.onreadystatechange = function(){ if (httpRequest.readyState == 4){ if(httpRequest.status == 200){ el.innerHTML = httpRequest.responseText; cb.call(this); } else { //alert("Error "+ httpRequest.status +":"+ httpRequest.statusText); } } } httpRequest.send(null); }; }; // Metoda na zjistÄ›nĂ tĹ™Ădy, vracĂ pole getClass = function(sc,get){ var el = new Array(); if(!get) get = document; var tagsall = get.getElementsByTagName('*'); var sc = sc.split(' '); for(i=0,j=0; i<tagsall.length; i++) { var element = " " + tagsall[i].className + " "; for(x=0;x<sc.length;x++) if (element.indexOf(' '+sc[x]+' ') != -1) el[j++] = tagsall[i]; } return el; } //Metoda Ar, volá funkce na hodnotách v poli Array.prototype.Ar = function(f, context) { if(!this || !(f instanceof Function)) return; for(var i=0,l=this.length;i!==l;i++) if(i in this) f.call(context, this[i], i, this); } //Metoda pro zjedodušenĂ˝ vypis mouseover a mouseout //fc1 = onmouseover, fc2 = onmouseout Element.prototype.mouse = function(fc1,fc2){ this.onmouseover = function(){ fc1.call(this); }; this.onmouseout = function(){ fc2.call(this); }; } |
||
joe Profil |
#2 · Zasláno: 27. 9. 2011, 00:15:04
Nebudu hodnotit přímo kód, protože až takový znalosti na to nemam.
1. Zlepšil bych komentáře, tohle luštit, to by se mi vážně nechtělo. V JavaScriptu se používá JSDoc a tady je nějaká špatná diakritika. 2. Názvy tříd a promenných nejsou podle původního očekávání. Od getClass bych očekával, že mi vrátí třídu (případně více tříd) u nějakého elementu / případně na tom, kde ji zavolám pokud by byla přidaná přes prototyp. Pole bych určitě neočekával. Navíc si nejsem nějak jistý, jestli funguje opravdu tak, jak očekáváš. 3. onload = ... Tady jsem váhal, asi moje neznalost, ale nepřijde mi to čitelné. To je asi událost celého okna, tedy psal bych window.onload = ... 4. Mezi =, &, +, ... je vhodné z obou stran psát mezeru, pokud to nahustíš na sebe, je to nepřehledné. 5. V dnešní době už já osobně hash nepoužívám a raději volím HTML 5 API, které mění adresu celou a ne jen za hashem. 6. Proč některé funkce zapisuješ jako xxx (zde navíc bez var) = function a jiné function xxx() ? 7. Metoda Element.prototype.mouse mi přijde k ničemu, co kdybys chtěl přidat události mousewheel, mouseup a jiné? |
||
John58 Profil * |
#3 · Zasláno: 27. 9. 2011, 00:27:45
1) Je to ok. Po zkopirováni to blbne. Nejspiš tim že použivam kodovani UTF8
3) Ok, a je tam nějaky rozdil, podlěmě by to mělo volat tak jak tak na objectu window. 4) Tak o přehlednost mi nejde a mě to vyhovuje, stejně to budu minifikovat nakonci. 5) Funguje i ve starších verzích IE? 7) Ja vim, je to taková v celku k ničemu funkce. Ale nadruhou stranu to nemusím vypisovat několikrat. |
||
_es Profil |
#4 · Zasláno: 27. 9. 2011, 07:42:43
John58:
„Ok, a je tam nějaky rozdil, podlěmě by to mělo volat tak jak tak na objectu window.“ Teoreticky môže byť, podobne ako pri vlastnosti window.name (príklad a). V starších IE, ani v IE 8, vôbec neexistuje globálna vlastnosť Element. To by si mal však testovať ty, ako výrobca tej „knižnice“. |
||
Časová prodleva: 13 let
|
0