zoukankan      html  css  js  c++  java
  • 王雨的JavaScript练习03---Ajax初体验

    AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。

    AJAX 不是新的编程语言,而是一种使用现有标准的新方法。

    AJAX 最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容。

    AJAX 不需要任何浏览器插件,但需要用户允许JavaScript在浏览器上执行。

    html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <div id="new"></div>
    <script src="script/addLoadEvent.js"></script>
    <script src="script/getHTTPObject.js"></script>
    <script src="script/getNewContent.js"></script>
    </body>
    </html>

    js:

    function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                oldonload();
                func();
            }
        }
    }
    
    function getHTTPObject() {
        if (typeof XMLHttpRequest == "undefined")
            XMLHttpRequest = function () {
                try { return new ActiveXObject("Msxml2,XMLHTTP.6.0"); }
                catch (e) {}
                try { return new ActiveXObject("Msxml2,XMLHTTP.3.0"); }
                catch (e) {}
                try { return new ActiveXObject("Msxml2,XMLHTTP"); }
                catch (e) {}
                return false;
            }
            return new XMLHttpRequest();
    }
    
    function getNewContent() {
        var request = getHTTPObject();
        if (request){
            request.open("GET","example.txt",true);
            request.onreadystatechange = function () {
                if (request.readyState == 4){
                    var para = document.createElement("p");
                    var txt = document.createTextNode("request.responseText");
                    para.appendChild(txt);
                    document.getElementById("new").appendChild(para);
                }
            };
            request.send(null);
        }else {
            alert('抱歉,你的浏览器不支持XMLHttpRequest')
        }
    }
    addLoadEvent(getNewContent);
  • 相关阅读:
    java_oop_方法2
    POJ 3276 Face The Right Way(反转)
    POJ 3276 Face The Right Way(反转)
    POJ 2566 Bound Found(尺取法,前缀和)
    POJ 2566 Bound Found(尺取法,前缀和)
    POJ 3320 Jessica's Reading Problem(尺取法)
    POJ 3320 Jessica's Reading Problem(尺取法)
    POJ 3061 Subsequence(尺取法)
    POJ 3061 Subsequence(尺取法)
    HDU 1222 Wolf and Rabbit(欧几里得)
  • 原文地址:https://www.cnblogs.com/blogwy/p/6858699.html
Copyright © 2011-2022 走看看