Ciao.
Stavo cercando di affinare la mia
conoscenza di Ajax su devarticles ho
trovato questo tutorial (leggermente modificato).
La cosa che non mi spiego è lo scope della variabile
data=receiveReq.responseXML.getElementsByTagName(' message');
(Per come la vedo io l'avrei dichiarata all'interno di displayData())
che per quanto ne so dovrebbe essere visibile solo all'interno
della funzione stateChecker() mentre risulta visibile
anche in displayData().
Allego il codice.
Codice PHP:
<script language="JavaScript" type="text/JavaScript">
function getXmlHttpRequestObject() {
var receiveReq=false; //Clear our fetching variable
try {
receiveReq = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
} catch (e) {
try {
receiveReq = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
} catch (e) {
receiveReq = false;
}
}
if (!receiveReq && typeof XMLHttpRequest!='undefined') {
receiveReq = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
}
return receiveReq;
}
var receiveReq = getXmlHttpRequestObject();
function getNews( doc )
{
//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
if (receiveReq.readyState == 4 || receiveReq.readyState == 0)
{
//Setup the connection as a GET call to your page
//True explicity sets the request to asyncronous (default).
receiveReq.open('GET', doc , true);
//Set the function that will be called when the XmlHttpRequest objects state changes.
receiveReq.onreadystatechange = stateChecker;
//Make the actual request.
receiveReq.send(null);
}
}
function stateChecker()
{
// if request is completed
if(receiveReq.readyState==4)
{
// if status == 200 display text file
if(receiveReq.status==200)
{
// create data container
createDataContainer();
// read XML data
data=receiveReq.responseXML.getElementsByTagName('message');
// display XML data
displayData();
}
else
{
alert('Failed to get response :'+ receiveReq.statusText);
}
}
}
function createDataContainer()
{
var div=document.getElementById('container');
if(div){return};
//document.getElementsByTagName('body')[0].removeChild(div);
var div=document.createElement('div');
div.setAttribute('id','container');
document.getElementsByTagName('body')[0].appendChild(div);
}
function displayData()
{
// reset data container
document.getElementById('container').innerHTML='';
var ul=document.createElement('ul');
for(var i=0;i<data.length;i++)
{
// create links
var li=document.createElement('li');
var a=document.createElement('a');
// assign 'href' attribute
a.setAttribute('href',data[i].getElementsByTagName('url')[0].firstChild.nodeValue);
// add link labels
a.appendChild(document.createTextNode(data[i].getElementsByTagName('title')[0].firstChild.nodeValue));
li.appendChild(a);
ul.appendChild(li);
}
document.getElementById('container').appendChild(ul);
}
window.onload = function()
{
getNews("news.xml");
}
</script>
Mi sono perso qc
sullo scope delle variabili in JS ?