new XMLHttpRequest()和頁面關係

1.  三個頁面分別對應"本身「的異步對象(3個)javascript

    <title></title>
    <script type="text/javascript">
        
        function GetXHR() {
            return new XMLHttpRequest();
        };
        var po = function () {
            var xhr = GetXHR();
            xhr.open("get", "PageOne.ashx", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("po").innerHTML = xhr.response;
                }
            };
            xhr.send(null);
        };
        var pt = function () {
            var xhr = GetXHR();
            xhr.open("get", "PageTwo.ashx", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("pt").innerHTML = xhr.response;
                }
            };
            xhr.send(null);
        };
        var pth = function () {
            var xhr = GetXHR();
            xhr.open("get", "PageThree.ashx", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("pth").innerHTML = xhr.response;
                }
            };
            xhr.send(null);
        };
        window.onload = function () {
            pth();//Three
            po();//One
            pt();//Two
        };
    </script>
</head>
<body>
    <div id="po" style="background-color: #ffd800;"></div>
    <div id="pt" style="background-color: #4cff00;"></div>
    <div id="pth" style="background-color: #b6ff00;"></div>
</body>
   //另外兩個頁面相似。
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //System.Threading.Thread.Sleep(1000); context.Response.Write("Hello World"); context.Response.Write("<br/>This is PageOne !!! <br/>" + "Second: " + DateTime.Now.Second + "<br/>Millisecond: " + DateTime.Now.Millisecond); }

結果:java

改成一個對象異步

    <script type="text/javascript">
        var xhr = false;
        function GetXHR() {
            return new XMLHttpRequest();
        };
        var po = function () {
            //var xhr = GetXHR();
            xhr.open("get", "PageOne.ashx", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("po").innerHTML = xhr.response;
                }
            };
            xhr.send(null);
        };
        var pt = function () {
            //var xhr = GetXHR();
            xhr.open("get", "PageTwo.ashx", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("pt").innerHTML = xhr.response;
                }
            };
            xhr.send(null);
        };
        var pth = function () {
            //var xhr = GetXHR();
            xhr.open("get", "PageThree.ashx", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("pth").innerHTML = xhr.response;
                }
            };
            xhr.send(null);
        };
        window.onload = function () {
            xhr = GetXHR();
            pth();//Three
            po();//One
            pt();//Two
        };
    </script>
</head>
<body>
    <div id="po" style="background-color: #ffd800;"></div>
    <div id="pt" style="background-color: #4cff00;"></div>
    <div id="pth" style="background-color: #b6ff00;"></div>
</body>

結果:spa

調試:3d

請求都進目標頁面了,卻」沒帶回來「數據調試

若是慢慢的調試:code

結果同樣對象

一個對象(簡短期)屢次請求同一個頁面blog

 

   <script type="text/javascript">
        var xhr = false;
        function GetXHR() {
            return new XMLHttpRequest();
        };
        var po = function () {
            //var xhr = GetXHR();
            xhr.open("get", "PageOne.ashx", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("po").innerHTML = xhr.response;
                }
            };
            xhr.send(null);
        };
        var pt = function () {
            //var xhr = GetXHR();
            xhr.open("get", "PageOne.ashx", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("pt").innerHTML = xhr.response;
                }
            };
            xhr.send(null);
        };
        var pth = function () {
            //var xhr = GetXHR();
            xhr.open("get", "PageOne.ashx", true);
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    document.getElementById("pth").innerHTML = xhr.response;
                }
            };
            xhr.send(null);
        };
        window.onload = function () {
            xhr = GetXHR();
            pth();//Three
            po();//One
            pt();//Two
        };
    </script>
</head>
<body>
    <div id="po" style="background-color: #ffd800;"></div>
    <div id="pt" style="background-color: #4cff00;"></div>
    <div id="pth" style="background-color: #b6ff00;"></div>
</body>

 

不一樣對象請求相同頁面:ip

相關文章
相關標籤/搜索