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.
Enjoy!
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>
<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:
thanks it really helpful
it reloads the whole page...
how to refresh only div, it refresh whole page and brings whole page inside that div
Post a Comment
Share your thoughts here...