首页 > 其他 > 详细

Ajax出入江湖

时间:2014-04-16 20:34:36      阅读:486      评论:0      收藏:0      [点我收藏+]

 

bubuko.com,布布扣
window.onload = initAll;
var xhr = false;

function initAll() {
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else {
        if (window.ActiveXObject) {
            try {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }

    if (xhr) {
        getPix();
    }
    else {
        alert("Sorry, but I couldn‘t create an XMLHttpRequest");
    }
}

function getPix() {
    xhr.open("GET", "flickrfeed.xml", true);
    xhr.onreadystatechange = showPictures;
    xhr.send(null);

    setTimeout(getPix, 5 * 1000);
}

function showPictures() {
    var tempText = document.createElement("div");
            
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var allImages = xhr.responseXML.getElementsByTagName("content");
            var randomImg = Math.floor(Math.random() * allImages.length);

            tempText.innerHTML = getPixVal(allImages[randomImg]);
            var thisImg = tempText.getElementsByTagName("p")[1];
            document.getElementById("pictureBar").innerHTML = thisImg.innerHTML;
        }
        else {
            alert("There was a problem with the request " + xhr.status);
        }
    }
    
    function getPixVal(inVal) {
        return (inVal.textContent) ? inVal.textContent : inVal.text;
    }
}
View Code

 

 

Ajax出入江湖,布布扣,bubuko.com

Ajax出入江湖

原文:http://www.cnblogs.com/easy5weikai/p/3666038.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!