עבר עריכה לאחרונה בתאריך 13.04.07 בשעה 01:15 על-ידי akoka (מפקח)
אוקי אז כולם יודעים מה זה AJAX , הכנתי לכם כלי שיעזור לכם להשתמש בו .
זה ממש פשוט , תסתכלו על הדוגמא ותבינו .
פונקציה אחת שעושה את הקריאה הAJAXית ומחזירה את התשובה לפונקציה שגם את הפונקציה הזאת שולחים לפונקציה שעושה את הAJAX.
אם לא תבינו את הדוגמא תשאלו .זה הקובץ הראשי :
http://rotter.name/User_files/nor/461d08336af43c7b.txt
הקוד של הקובץ הראשי
<html> <head> <script> //----------------------------create the xml http object function get_http_request_obj(){ var l_xmlDoc = new ActiveXObject("Msxml2.XMLHTTP"); return l_xmlDoc } //---------------------------generic function the create the request and redirect the answer to the givven function function make_request(postTo,callBackFunc){ var xmlDocRes = get_http_request_obj(); xmlDocRes.onreadystatechange = function() { if (xmlDocRes.readyState == 4) if (xmlDocRes.status == 200) { eval(callBackFunc); // call to the givven function } else { alert('There was a problem with the request,Please try again later.'); } } xmlDocRes.open('GET', postTo, true); xmlDocRes.send(null); } //--------------------when call_ajax_1 finish , make_request redirect to this funcion function ajax_1_return(str){ document.getElementById("text1").value=str; } //---------------------when call_ajax_2 finish , make_request redirect to this funcion function ajax_2_return(str){ document.getElementById("text2").value=str; } //--------------set up the rapper and call to the ajax make_request function that make the ajax request function call_ajax_1(){ urlTo='http://rotter.name/User_files/nor/461d05575d4dcaf9.txt'; make_request(urlTo,"ajax_1_return(xmlDocRes.responsetext);") } //----------------set up the rapper and call to the ajax make_request function that make the ajax request function call_ajax_2(){ urlTo='http://rotter.name/User_files/nor/461d057b5df1a6ae.txt'; make_request(urlTo,"ajax_2_return(xmlDocRes.responsetext);") } </script> </head> <body> <input type="button" value="ajax1" onclick="call_ajax_1();"><input type="text" id="text1"> <input type="button" value="ajax2" onclick="call_ajax_2();"><input type="text" id="text2"> </body> </html>
|
הקובץ הראשי קורא ל2 הקבצים הבאים
http://rotter.name/User_files/nor/461d05575d4dcaf9.txt
http://rotter.name/User_files/nor/461d057b5df1a6ae.txt
עכשיו תחשבו שאת הפונקציות get_http_request_obj
make_request
אתם סוגרים בקובץ אחר , וזהו יש לכם מודול מוכן שמטפל בAJAX אסיכרוני.
תהנו.
פונקצית תאימות לדפדפנים
<script type="text/javascript"> function ajaxengine() { var xhr; try { xhr=new XMLHttpRequest(); } catch (e) { try { xhr=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xhr=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { return false; } } } } </script>
|
