[Ajax] Hello World


<html>
    <head>
        <title>Test Ajax</title>
        <script language="JavaScript" type="text/javascript">
            
            // check browser
            function getXmlHttpRequestObject() {
                if (window.XMLHttpRequest) {
                    return new XMLHttpRequest();
                } else if (window.ActiveXObject) {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                } else {
                    alert ("not support");
                }
            }
            
            // Get browser Object
            var receiveReq = new getXmlHttpRequestObject();
            
            function sayHello() {
                //  .readyState = Attribute ( the type of unsigned short )
                // 0 = Not work
                // 1 = process
                // 2 = compleate process
                // 3 = server process
                // 4 = server complete

                if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {

                    receiveReq.open("GET", 'test.html', true);
                    // .onreadystatechange = Attribute ( represent a fuction that must be invoked when readyState changes value )
                    receiveReq.onreadystatechange = handleSayHello;
                    receiveReq.send(null);
                    // send content
                }
            }
            
            
            function handleSayHello() {
                if (receiveReq.readyState == 4) {
                    document.getElementById('span_result').innerHTML = receiveReq.responseText;
                    // Receive data from server as text
                }
            }
            
        </script>
    </head>
    <body>
        <a href="javascript:sayHello();">Say Hello</a><br />
        <span id="span_result"></span>
    </body>
</html>


Ref :
http://campus.en.kku.ac.th/campusboard/viewtopic.php?p=4279
http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/
http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getelementbyid.asp
http://www.dynamicajax.com/fr/AJAX_Hello_World-271_290_322.html
คำสำคัญ (Tags): #programming#ajax
หมายเลขบันทึก: 44599เขียนเมื่อ 13 สิงหาคม 2006 21:44 น. ()แก้ไขเมื่อ 7 มิถุนายน 2012 12:40 น. ()สัญญาอนุญาต: จำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (0)

ไม่มีความเห็น

พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท