Auto refresh div using Ajax | Coding Cluster - using asp.net, c#, mvc 4, iphone, php, ios, javascript, in asp.net mvc 3 & more
 

Auto refresh div using Ajax

Saturday

Refreshing a div without page reload using Ajax:

This Ajax code will used to refresh a div element automatically in every 3 seconds without page reload.
You can changed the refreshing time.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Auto Refresh</title>
    <script type="text/javascript">
      function AutoRefresh(){
        var xmlHttp;
        try{
          xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
        }
        catch (e){
          try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
          }
          catch (e){
            try{
              xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e){
              alert("No AJAX");
              return false;
            }
          }
        }

        xmlHttp.onreadystatechange=function(){
          if(xmlHttp.readyState==4){
            document.getElementById('AutoUpdte').innerHTML=xmlHttp.responseText;
            setTimeout('AutoRefresh()',3000); // JavaScript function calls AutoRefresh() every 3 seconds
          }
        }
        xmlHttp.open("GET","Your_page_url_that_contains_the_div_content",true);
        xmlHttp.send(null);
      }

      AutoRefresh();
    </script>
  </head>
  <body>
    <div id="AutoUpdte">Your Content</div>
  </body>
</html>

Enjoy!

3 comments:

Anonymous said...

thanks it really helpful

Mike said...

it reloads the whole page...

Smitha said...

how to refresh only div, it refresh whole page and brings whole page inside that div

Post a Comment

Share your thoughts here...

 
 
 

RECENT POSTS

Boost

 
Blogger Widgets