Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    Ajax js scope variabile .........

    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 
    getNewsdoc 
    {
        
    //If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
        
    if (receiveReq.readyState == || 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 ?

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  2. #2
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649

    Re: Ajax js scope variabile .........

    Originariamente inviato da whisher
    Mi sono perso qc sullo scope delle variabili in JS ?
    se ho capito bene, cosi' potrebbe essere

    data e' sicuramente globale

    codice:
    function createDataContainer()
    {
        var div=document.getElementById('container'); 
    ...
    qui x es. div e' locale (ha il var davanti e si trova in funzione)
    tanto che in altra funzione
    codice:
    function displayData()
    {
        // reset data container
        document.getElementById('container').innerHTML='';
    ...
    non puo' usare div.innerHTML='';

  3. #3

    ............

    Grazie della reply ma....
    Uhm però in questo esempio
    la sola globale è data0:
    Codice PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"
    >
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <
    script language="JavaScript" type="text/JavaScript">
    var 
    data0=document.getElementsByTagName('p');
    function 
    uno()
    {
        
    data1=document.getElementsByTagName('p');
        var 
    data2=document.getElementsByTagName('p');
            
        
    }

    function 
    due()
    {
        
    alert(data0.length);
        
    alert(data1.length);
        
    alert(data2.length);
    }
    window.onload = function()
    {
    due();
    }
    </script>

    </head>

    <body>


    uno</p>


    due</p>


    tre</p>
    </body>
    </html> 
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  4. #4

    ...........

    Ecco scoperto l'arcano e
    Codice PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"
    >
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <
    script language="JavaScript" type="text/JavaScript">
    var 
    data0=document.getElementsByTagName('p');
    function 
    uno()
    {
        
    data1=document.getElementsByTagName('p');
        var 
    data2=document.getElementsByTagName('p');
        
    due();
        
    }

    function 
    due()
    {
        
    alert(data0.length);
        
    alert(data1.length);
        
    alert(data2.length);
    }
    window.onload = function()
    {
    uno();
    }
    </script>

    </head>

    <body>


    uno</p>


    due</p>


    tre</p>
    </body>
    </html> 
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  5. #5
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    bravo
    ...cmq anche data1 e' globale in tutti e 2 i casi...

  6. #6

    .......


    Codice PHP:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"
    >
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <
    script language="JavaScript" type="text/JavaScript">
    var 
    data0=document.getElementsByTagName('p');
    function 
    uno()
    {
        
    data1=document.getElementsByTagName('p');
        var 
    data2=document.getElementsByTagName('p');
    }

    function 
    due()
    {
        
    alert(data0.length);
        
    alert(data1.length);
        
    alert(data2.length);
    }
    window.onload = function()
    {
    uno();
    due();
    }
    </script>

    </head>

    <body>


    uno</p>


    due</p>


    tre</p>
    </body>
    </html> 

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.