<< 程序员对索引的误解 | 首页 | 海量数据处理常用思路和方法 >>

通过XMLHTTP加载HTML文件

<html>
    <head>
        <script type="text/javascript">
            var xmlhttp;
            function loadXMLDoc(url){
                xmlhttp = null;
				// code for Firefox, Opera, IE7, etc.
                if (window.XMLHttpRequest) {
					xmlhttp = new XMLHttpRequest();
				}
				else {
					// code for IE6, IE5
					if (window.ActiveXObject) {
						xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
				}
                if (xmlhttp != null) {
                    xmlhttp.onreadystatechange = state_Change;
                    xmlhttp.open("GET", url, true);
                    xmlhttp.send(null);
                }
                else {
                    alert("Your browser does not support XMLHTTP.");
                }
            }
            
            function state_Change(){
				// 4 = "loaded"
                if (xmlhttp.readyState == 4) {
					// 200 = "OK"
                    if (xmlhttp.status == 200) {
                        document.getElementById('T1').innerHTML = xmlhttp.responseText;
                    }
                    else {
                        alert("Problem retrieving data:" + xmlhttp.statusText);
                    }
                }
            }
        </script>
    </head>
    <body onload="loadXMLDoc('test.html')">
        <div id="T1" style="border:1px solid black;height:40;width:300;padding:5">
        </div>
        <br/>
        <button onclick="loadXMLDoc('test.html')">
            Click
        </button>
    </body>
</html>
标签 : , ,



发表评论 发送引用通报