Pacecode Blog Tips, Scripts and More…

21Sep/082

Part of Page refresh using AJAX with timed delay

<head>
<script type="text/javascript">
function makAx() // this function to activate XMLHttpRequest Object
{
try
{
xm = new ActiveXObject("Msxml2.XMLHTTP");
return true;
}
catch (e)
{
try
{
xm = new ActiveXObject("Microsoft.XMLHTTP");
return true;
}
catch (e2)
{
xm = false;
}
}
if (!xm && typeof XMLHttpRequest != ‘undefined’)
{
xm = new XMLHttpRequest();
return true;
}
}
function showImages()
{
if(makAx()) // XMLHttpRequest checked here
{
url = "external-page-for-db-query.php"; // this file will contains your db query
callavail();
}
}
function callavail()
{
xm.open("GET",url,true); // This is ajax functionality. It may have true(for synchronous request) or false(for asynchronous request)
xm.onreadystatechange = cavail;
xm.send(null);
}
function cavail()
{
if(xm.readyState == 4) // If ready state reached
{
var response = xm.responseText;
var str = document.getElementById("showResult"); // result of "external-page-for-db-query.php" will come here
str.innerHTML = response;
setTimeout("showImages()",1000); // here 1000 represents one second for time delay. showImage() function will be called with the specified time delay
}
}
</script>
</head>

<body>
<div id="showResult"></div> <!–page result will be displayed here–>
</body>
</html>

21Sep/080

Part of page refresh with time delay using MockAJAX – Facebook Developers

A Simple script that make our application works great. Yes, The following FBJS - FaceBook JavaScript will help you for part of page refresh in the facebook applications.

Uses:

1) Helps to retrieve data from your server database without refreshing the page
2) It would be useful for cricket scores and other live datas.

Mock AJAX Script as follows,

<script>

<!–
function partRefresh(){

var ajax = new Ajax();

ajax.responseType = Ajax.FBML;

ajax.ondone = function(data) {

document.getElementById(’partId’).setInnerFBML(data);

setTimeout(function() {partRefresh()},60000); // Making time delay

}
ajax.post("http://www.example.com/filename.php");
}
//–>
</script>

<div id="partId"></div> // the output of filename.php is displayed here
<script>
partRefresh(); // Just calling the function on browser loads
</script>

Filed under: Uncategorized No Comments