美文网首页
四周搞定《JavaScript 高级程序设计》 - 读书笔记(D

四周搞定《JavaScript 高级程序设计》 - 读书笔记(D

作者: 显卡84du | 来源:发表于2018-12-16 01:21 被阅读6次

第21章 Ajax 与 Comet

21.1 XMLHttpRequest 对象

21.1.3 GET 请求

以下函数可以辅助向现有 URL 的末尾添加查询字符串参数。

function addURIParam(url, name, value) {
    url += (url.indexOf("?") == -1 ? "?" : "&");
    url += encodeURIComponent(name) + "=" + encodeURIComponent(value);
    return url;
}

21.3.2 progress 事件

根据 event 对象的三个属性,可以为用户创建一个进度指示器。

var xhr = createXHR();

xhr.onload = function (event) {
    if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
        alert(xhr.responseText);
    } else {
        alert("Request was unsuccessful: " + xhr.status);
    }
};

xhr.onprogress = function (envet) {
    var divStatus = document.getElementById("status");
    if (event.lengthComputable) {
        divStatus.innerHTML = "Received " + event.position + " of " + event.totalSize + " bytes";
    }
};

xhr.open("get", "url.php", true);
xhr.send(null);

相关文章

网友评论

      本文标题:四周搞定《JavaScript 高级程序设计》 - 读书笔记(D

      本文链接:https://www.haomeiwen.com/subject/fgtzhqtx.html