des Posted January 5, 2007 Report Share Posted January 5, 2007 doma aptuveni taada: ajax.js: //the requests reqs = new Array(); //single request function AjaxRequest(req, func, id) { this.req = req; this.func = func; this.id = id; } function execRequests() { for (var i = 0; i < reqs.length; i++) { if (reqs[i].req.readyState == 4 && (reqs[i].req.status == 200 || reqs[i].req.status == 304)) { reqs[i].func(reqs[i].req.responseText, reqs[i].id); reqs[i].req = null; reqs.splice(i,1); } } } //ajax constructor function Ajax() { } Ajax.prototype.createRequest = function() { try{ // Opera 8.0+, Firefox, Safari return new XMLHttpRequest(); } catch (e) { // Internet Explorer Browsers try{ return ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { // Something went wrong alert("Your browser does not support Ajax. Get FireFox! www.mozilla.com"); return false; } } } } Ajax.prototype._rndsid = function(url) { if (url.indexOf('?') == -1) return url + "?___ajaxsid=" + Math.random(); return url + "&___ajaxsid=" + Math.random(); } Ajax.prototype.doreq = function(method, url, para, func, id, timer) { var request = this.createRequest(); if (!request) return false; request.onreadystatechange = execRequests; request.open(method, this._rndsid(url), true); if (method == "POST") { request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); request.setRequestHeader("Content-length", para.length); request.setRequestHeader("Connection", "close"); request.send(para); } else { request.send(null); } reqs.push(new AjaxRequest(request, func, id)); if (timer) { var _this = this; setTimeout(function(){_this.doreq(method, url, para, func, id, timer);}, timer); } } Ajax.prototype.get = function(url, func, id, timer) { this.doreq("GET", url, "", func, id, timer); } Ajax.prototype.post = function(url, para, func, id, timer) { this.doreq("POST", url, para, func, id, timer); } //fill Ajax.prototype.doFill = function(res, id) { document.getElementById(id).innerHTML = res; var js = new RegExp('(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', 'img'); var matches = res.match(js); if (matches) for (i in matches) eval(matches[i].replace(/<script.*?>/g, "").replace(/<\/script>/g, "")); } Ajax.prototype.getFill = function(url, id, timer) { this.get(url, this.doFill, id, timer); } Ajax.prototype.postFill = function(url, para, id, timer) { this.post(url, para, this.doFill, id, timer); } //eval Ajax.prototype.doEval = function(res) { eval(res); } Ajax.prototype.getEval = function(url) { this.get(url, this.doEval); } Ajax.prototype.postEval = function(url, para) { this.post(url, para, this.doEval); } ajax = new Ajax(); testmyajax.html (jā, ne tuvu nav valīds :D): <html> <head> <script language="javascript" type="text/javascript" src = "ajax.js"></script> </head> <div id = "res"> </div> <div id = "a"> </div> <div id = "b"> </div> <script language="javascript" type="text/javascript"> ajax.getFill("rnd.php", "res", 1000); </script> </body> </html> un rnd.php: <?php echo rand(1, 5000); ?> <script> document.getElementById("a").innerHTML = "aa"; </script> <script language="javascript" type="text/javascript"> document.getElementById("b").innerHTML = "bb"; </script> Ajax.doFill metode noevalo visus skriptus arī, ja tādi ir. Link to comment Share on other sites More sharing options...
black Posted January 6, 2007 Report Share Posted January 6, 2007 (edited) Man patika doma par request queue un timeoutiem. Bet... Es nekur neredzu clearTimeout() metodi. doReq() funkcija tiek izsaukta vēlreiz, request queue tiek pievienots vēl viens tāds pats requests ar ("reqs.push(...)"), un esam dabūjuši - pašā sliktākajā gadījumā - bezgalīgu ciklu, labākajā - vairākas reizes izsauktu vienu un to pašu requestu. Edited January 7, 2007 by black Link to comment Share on other sites More sharing options...
Lynx Posted January 6, 2007 Report Share Posted January 6, 2007 (edited) Ir iespējams iztikt bez eval, es savā jaunākajā uz Ajax bāzētajā projektā izmantoju šādu metodi: Lapā, kura tiek atgriezta no ajax pieprasījuma pašā augšā ir: <div id="include_js" style="display:none;">alert('aa');</div> Skripts kurš veic ajax pieprasījumu pēc pozitīvas atbildes saņemšanas pārbauda, vai eksistē include_js un, ja eksistē ieliek sourcā: if (request.readyState == 4 && request.status == 200) { if (document.getElementById('include_js')) { var head = document.getElementsByTagName("head")[0]; script = document.createElement('script'); script.id = 'upload'; script.type = 'text/javascript'; //script.src = document.getElementById("include_js").innerHTML; // izmantojot šo pašu metodi varam inkludot arī veselu sourci head.appendChild(script) document.getElementById(script.id).innerHTML = document.getElementById("include_js").innerHTML; } } Testējis esmu gan uz FF 2.0 tikai. Edited January 6, 2007 by Lynx Link to comment Share on other sites More sharing options...
bubu Posted January 6, 2007 Report Share Posted January 6, 2007 innerHTML ar nav labi lietot. Tas var sačakarēt DOM koku, tb sataisī ne īsti tādu kā gribi. Link to comment Share on other sites More sharing options...
foxsk8 Posted May 24, 2007 Report Share Posted May 24, 2007 Man tagad ir tieši tāda paša problēma, kā lai ar ajax panāk to, ka izsauc javascript failu. Man ir vajadzīgi pēdējie topiki no furuma, bet tie tiek ģenērēti javascriptā. Itkā bez javascript visu dabūju. Ir kādi varianti? Link to comment Share on other sites More sharing options...
bubu Posted May 24, 2007 Report Share Posted May 24, 2007 eval(skripts_kā_strings); Link to comment Share on other sites More sharing options...
foxsk8 Posted May 24, 2007 Report Share Posted May 24, 2007 Būtu labi, ja es kaut cik saprastu. Vajag tikai lai pēdējie topiki ar ajax refreshotos. Itkā izdomāju savādāku variantu, bet nesaprotu kāpēc encoding nestrādā. skatījos arī citus variantus, tas pats. Šiet ir kods http://paste.php.lv/5579 Ja dabotu valodu visu kā vajag, tad mana problēma būtu atrisināta. portal.php failam encodings ir windows-1257, no kurienes tad viņs ņem to encodingu, kas sorce ir? Link to comment Share on other sites More sharing options...
andrisp Posted May 25, 2007 Report Share Posted May 25, 2007 Pati lapa ir win1257 ? Link to comment Share on other sites More sharing options...
foxsk8 Posted May 25, 2007 Report Share Posted May 25, 2007 Jā, tas viss ir vajadzīgs priekš phpbb foruma, tas ajax. Protams šo to esmu samodificējis pats. Adrese ir www.notepad.lv Foruma lang fails ir win1257. Bet tas nebūtu tik svarīgi, nelietojot nemaz forumu, arī prosta testu ņemot, get info no faila, viņs nerāda ierakstus latviešu valodā. Tāka vaina varētu būt tajā ajaxa. Link to comment Share on other sites More sharing options...
andrisp Posted May 25, 2007 Report Share Posted May 25, 2007 Tajā kodā nevarētu būt vainas (manu prāt). Pamēģini no tā pieprasāmā faila vēl papildus nosūtīt HTTP headeri Content-Type: text/tips; charset=windows-1257 Link to comment Share on other sites More sharing options...
foxsk8 Posted May 25, 2007 Report Share Posted May 25, 2007 Kādu koda paraugu varētu parādīt? Vienkārši, nav sajēgas vairāk, pēc vakardienas meklējumiem līdz pulks. 2 naktī. Link to comment Share on other sites More sharing options...
andrisp Posted May 25, 2007 Report Share Posted May 25, 2007 header fja. Link to comment Share on other sites More sharing options...
foxsk8 Posted May 25, 2007 Report Share Posted May 25, 2007 Jā. Lai strādātu lv valoda. Link to comment Share on other sites More sharing options...
andrisp Posted May 25, 2007 Report Share Posted May 25, 2007 Ko jā ? Pamēģini nosūtīt to headeri. Link to comment Share on other sites More sharing options...
foxsk8 Posted May 25, 2007 Report Share Posted May 25, 2007 Labi, paprovēšu. Redzēs kas sanāks. Link to comment Share on other sites More sharing options...
Recommended Posts