topical media & game development
basic-ajax-07-Polling.js / js
var xHRObject = false;
if (window.XMLHttpRequest)
{
xHRObject = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
}
function loadDocument(fileName) {
var xmlDoc = null
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("MSXML2.DOMDocument.3.0")
}
else
{
document.implementation.createDocument("","",null);
}
xmlDoc.async = false;
xmlDoc.load(fileName);
return xmlDoc;
}
function transformToHTML(xmlDoc, xslDoc) {
var html = "";
if (window.XSLTProcessor) {
var xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xslDoc);
var fragment = xsltProc.transformToFragment(xmlDoc, document);
html = new XMLSerializer().serializeToString(fragment);
} else if (window.ActiveXObject) {
html = xmlDoc.transformNode(xslDoc);
}
return html;
}
function getData()
{
//Check to see if the XMlHttpRequest object is ready and whether it has
//returned a legitmate response
if (xHRObject.readyState == 4 && xHRObject.status == 200)
{
// Load XML
var xml = loadDocument("Stocks.xml");
//Load XSL
var xsl = loadDocument("Stocks.xsl");
//Transform
document.getElementById("Stocks").innerHTML = transformToHTML(xml, xsl);
//Clear the object and call the getDocument function in 5 seconds
setTimeout("getDocument()", 5000);
}
}
function getDocument()
{
xHRObject.open("GET", "GetStocksList.php?id=" + Number(new Date()), true);
xHRObject.onreadystatechange = getData;
if (xHRObject.overrideMimeType) {
xHRObject.overrideMimeType("text/plain");
}
xHRObject.send(null);
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.