1

如何以时间间隔(不是整个页面)刷新网页的特定部分?

4

3 回答 3

2

You can use Ajax for your purpose. suppose you want to check username availability before registering a user to your site.

create a request object asynchronously

function createRequest()

{

try{

 request=new XMLHttpRequest();

} catch(tryMS){

    try{

        request=new ActiveXObject("Msxml2.XMLHTTP");

    } catch(otherMS){

         try{

             request=new ActiveXObject("Microsoft.XMLHTTP");

         } catch(failed) {

             request=null;
         }

    }

}

return request;

}

Next is the code to send a asynchronous request

function checkAvailability (username) {

 request=createRequest();

 if(request==null){

   alert("Ajax request not possible on your browser");

   return;

 }

 var url="checkAvailability?username="+username;

 request.open("GET", url, true);

 request.onreadystatechange = showStatus;

 request.send(null);

}

Track the response

function showStatus () {

if(request.readyState == 4) {

if(request.status == 200) {

  var response = request.responseText;

          if(response == 1){

              //username available

          } else{

              //username not available

          } 

  }
   
}

}

于 2012-04-10T10:30:40.967 回答
2

假设您的网页中有一个要刷新的 DIV:

<div id="myDiv"> </div>

要使用 javascript 刷新它,您只需选择它并更改 html 代码:

document.getElementById("myDiv").innerHtml = "Your new html code to display"

如果您想处理表单、数据库查询...您必须使用 AJAX 来调用一些 php 脚本,例如无需重新加载当前页面...

于 2012-04-10T10:16:16.577 回答
1

你在谈论 AJAX

查看http://api.jquery.com/jQuery.ajax/的 jQuery

但请考虑学习底层的 javascript 语言 - 从长远来看,你会更好 这里是一个简单的例子 http://www.degraeve.com/reference/simple-ajax-example.php

ajax 背后的历史可以在这里找到http://www.adaptivepath.com/ideas/ajax-new-approach-web-applications

于 2012-04-10T10:17:01.897 回答