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

    Problema con funzione e parametro

    Ciao ragazzi,
    ho un problema con la funzione handleRequestStateChangePopolaTematiche.

    Sto facendo delle prove con ajax e ho adattato una funzione già esistente. Quello che non capisco è perché quando viene richiamata questa funzione non vengono messe le parentesi solite () e perché se le metto (e magari modifico la funzione per passare un parametro) non funziona più nulla.

    Qualcuno mi sa apiegare? Grazie

    codice:
    // read a file from the server
    function popolaTematica(numeroelemento)
    { 
      // only continue if xmlHttp isn't void
      if (xmlHttp)
      {
        // try to connect to the server
        try
        {
          var mainSelection = document.getElementById("id_tipologia_corso_"+ numeroelemento);
          // initiate reading a file from the server
          xmlHttp.open("POST", "tematica_corso.php", true);
          xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
          xmlHttp.onreadystatechange = handleRequestStateChangePopolaTematiche;
          xmlHttp.send("id_tipologia_corso=" + mainSelection.options[mainSelection.selectedIndex].value);
        }
        // display the error in case of failure
        catch (e)
        {
          alert("Can't connect to server:\n" + e.toString());
        }
      }
    }
    
    // function called when the state of the HTTP request changes
    function handleRequestStateChangePopolaTematiche()
    {
      // when readyState is 4, we are ready to read the server response
      if (xmlHttp.readyState == 4)
      {
        // continue only if HTTP status is "OK"
        if (xmlHttp.status == 200)
        {
          try
          {
            // do something with the response from the server
            handleServerResponsePopolaTematiche();
          }
          catch(e)
          {
            // display error message
            alert("Error reading the response: " + e.toString());
          }
        }
        else
        {
          // display status message
          alert("There was a problem retrieving the data:\n" +
                xmlHttp.statusText);
        }
      }
    }
    Disegnatori e romanzieri d'Italia, visitate il mio sito, cerchiamo favolieri dark ed illustratori dark fantasy... http://www.neilgaimania.it

  2. #2
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    perche' questa e' la sintassi
    window.onload=funzione;
    window.onload=function(){funzione(parametro);}

    ciao

  3. #3
    Grazie mille... ho fatto la modifica che mi suggerisci ma, anche se riesco a passare il parametro alla seconda funzione, lo script si blocca... Mentre se levo le modifiche, funge tutto normalmente.

    Perché?

    codice:
    // read a file from the server
    function popolaTematica(numeroelemento)
    { 
      // only continue if xmlHttp isn't void
      if (xmlHttp)
      {
        // try to connect to the server
        try
        {
          var mainSelection = document.getElementById("id_tipologia_corso_"+ numeroelemento);
          // initiate reading a file from the server
          xmlHttp.open("POST", "tematica_corso.php", true);
          xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
          xmlHttp.onreadystatechange = handleRequestStateChangePopolaTematiche;
          xmlHttp.onreadystatechange = handleRequestStateChangePopolaTematiche(numeroelemento);
          xmlHttp.send("id_tipologia_corso=" + mainSelection.options[mainSelection.selectedIndex].value);
        }
        // display the error in case of failure
        catch (e)
        {
          alert("Can't connect to server:\n" + e.toString());
        }
      }
    }
    
    // function called when the state of the HTTP request changes
    function handleRequestStateChangePopolaTematiche(numeroelemento)
    {
      // when readyState is 4, we are ready to read the server response
      if (xmlHttp.readyState == 4)
      {
        // continue only if HTTP status is "OK"
        if (xmlHttp.status == 200)
        {
          try
          {
            // do something with the response from the server
            handleServerResponsePopolaTematiche();
          }
          catch(e)
          {
            // display error message
            alert("Error reading the response: " + e.toString());
          }
        }
        else
        {
          // display status message
          alert("There was a problem retrieving the data:\n" +
                xmlHttp.statusText);
        }
      }
    }
    Disegnatori e romanzieri d'Italia, visitate il mio sito, cerchiamo favolieri dark ed illustratori dark fantasy... http://www.neilgaimania.it

  4. #4
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    vedo che e' ancora sbagliato
    xmlHttp.onreadystatechange = handleRequestStateChangePopolaTematiche(numeroelem ento);
    poi, forse non ci siamo capiti
    // se non devi passare valori come argomenti della funzione
    window.onload=funzione;

    // altrimenti
    window.onload=function(){funzione(parametro);}

  5. #5
    In effetti non avevo capito bene...

    Cmq contintuo ad essere confuso.

    xmlHttp.onreadystatechange = handleRequestStateChangePopolaTematiche(numeroelem ento);

    non è come il tuo

    variabile = funzione(parametro);

    ???

    scusami, sono una schiappa!
    Disegnatori e romanzieri d'Italia, visitate il mio sito, cerchiamo favolieri dark ed illustratori dark fantasy... http://www.neilgaimania.it

  6. #6
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    codice:
    xmlHttp.onreadystatechange = function(){handleRequestStateChangePopolaTematiche(numeroelemento)};

  7. #7
    Ah! Ok!

    quindi quando richiamo una funzione dentro un'altra funzione devo usare questa sintassi?

    grazie mille sei stato gentilissimo, buona domenica!
    Disegnatori e romanzieri d'Italia, visitate il mio sito, cerchiamo favolieri dark ed illustratori dark fantasy... http://www.neilgaimania.it

  8. #8
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    con i gestore evento (per questo facevo l' esempio dell' onload) non puoi assegnare una funzione come normalmente faresti, altrimenti viene eseguita immediatamente e non al verificarsi dell' evento

    finche' non devi passare valori come argomenti della funzione devi omettere le tonde,
    altrimenti puoi usare una funzione anonima, function(){...}

    ciao

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.