0

我有这个 javascript:

function createRequestObject(){
    var req;
    if(window.XMLHttpRequest){
        //For Firefox, Safari, Opera
        req = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){
        //For IE 5+
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        //Error for an old browser
        alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
    }
    //alert (req);
    return req;
}

//Make the XMLHttpRequest Object
var http = createRequestObject();

function sendRequestTwo(method, url, head1){
    head = head1
    if(method == "get" || method == "GET"){
        http.open(method,url);
        http.onreadystatechange = handleResponseTwo;
        http.send(null);

    }
}

var head; //divs name

function handleResponseTwo(){
    if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        if(response){  
            document.getElementById(head).innerHTML = response;
        }
    }      
}

这部分

            document.getElementById(head).innerHTML = response;

将结果粘贴回我的页面。如何在页面完全加载之前显示“正在加载”?

4

3 回答 3

3

如何在正文中加载一个带有 Html 的 Div

<div id="loadingDiv" class="hiddenClass">Fancy rotating image goes here</div>

有一个显示显示:无的 CSS 类;那么你可以在之前使用它

http.onreadystatechange = handleResponseTwo;
var myDiv = document.getElementById('loadingDiv');
myDiv.className = "showClass";

然后

document.getElementById(head).innerHTML = response;

添加

var myDiv = document.getElementById('loadingDiv');
myDiv.className = "hiddenClass";

全部一起

function createRequestObject(){
    var req;
    if(window.XMLHttpRequest){
        //For Firefox, Safari, Opera
        req = new XMLHttpRequest();
    }
    else if(window.ActiveXObject){
        //For IE 5+
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        //Error for an old browser
        alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
    }
    //alert (req);
    return req;
}

//Make the XMLHttpRequest Object
var http = createRequestObject();

function sendRequestTwo(method, url, head1){
    head = head1
    if(method == "get" || method == "GET"){
        var myDiv = document.getElementById('loadingDiv');
        myDiv.className = "showClass";

        http.open(method,url);
        http.onreadystatechange = handleResponseTwo;
        http.send(null);

    }
}

var head; //divs name

function handleResponseTwo(){
    if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        if(response){  
            document.getElementById(head).innerHTML = response;
            var myDiv = document.getElementById('loadingDiv');
            myDiv.className = "hiddenClass";
        }
    }      
}
于 2012-04-24T15:21:58.163 回答
1

我不知道您的 HTML 的详细信息,但是您不能将 loading 放入带有 的标签中id="head"吗?

然后,当您更新 innerHTML 时,加载将自动被替换。如果您有更多要求,请描述。

于 2012-04-24T15:10:29.413 回答
1

可以sendRequestTwo

于 2012-04-24T15:13:19.777 回答