ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • AJAX 시스템 시간 샘플
    Programming/AJAX 2011. 3. 2. 15:52


    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    .displaybox{
     width:150px;
     background-color:#fff;
     border:2px solid #000;
     padding:10px;
     font:24px normal verdana;
    }
    </style>
    <script type="text/javascript">
    //브라우저 종류 판별을 이용한 방법
    /*
    function getXMLHTTPRequest(){
     var req = false;
     if(window.XMLHTTPRequest){
      req = new XMLHTTPRequest();
     }else{
      if(window.ActiveXObject){
       try{
        req = new ActiveXObject("Msxml2.XMLHTTP");
       }catch(err){
        try{
         req = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(err){
         req = false;
        }
       }
      }
     }
     return req;
    }
    */
    // 브라우저에 무관하게 객체 생성
    function getXMLHTTPRequest(){
     var req = false;
     if(window.XMLHTTPRequest){
      req = new XMLHTTPRequest();
     }else{
      if(window.ActiveXObject){
       try{
        req = new ActiveXObject("Msxml2.XMLHTTP");
       }catch(err){
        try{
         req = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(err){
         req = false;
        }
       }
      }
     }
     return req;
    }

    var http = getXMLHTTPRequest();

    function getServerTime(){
     var myurl = "telltimeXML.php";
     myRand=parseInt(Math.random()*99999999);
     var modurl = myurl+"?rand="+myRand;
     http.open("GET",modurl,true);
     http.onreadystatechange = useHttpResponse;
     http.send(null);
    }


    function useHttpResponse(){

      if(http.readyState == 4){
       if(http.status ==200){
        var timeValue = http.responseXML.getElementsByTagName('timenow')[0];
        document.getElementById('showtime').innerHTML = timeValue.childNodes[0].nodeValue;
       }
      }else{
       document.getElementById('showtime').innerHTML = '<img src="amin.gif">';
      }
    }
    </script>
    </head>
    <body style="background-color:#ccc;" onload="getServerTime()">
    <center>
    <h1>Ajax Demonstration</h1>
    <form name="form1">
     <input type="button" value="테스트" onclick="getServerTime()">
     
    </form>
    <div id="showtime" class="displaybox"></div>
    </center>
    </body>
    </html>

Designed by Tistory.