Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [AJAX] Zmienna globalna przy HttpRequest
Forum PHP.pl > Forum > Przedszkole
Earth
Kod
var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject() {
  if (window.XMLHttpRequest) {
   xmlHttp = new XMLHttpRequest();
  } else {
   if (window.ActiveXObject) {
    try {
     XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
      for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
     } catch (e) {alert("Sorry, but I couldn't create an XMLHttpRequest");xmlHttp=false;}
   }
  }
  return xmlHttp;
}

function loadXmlHttpRequestObject(url) {
  if (xmlHttp) {
   try {
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = function () {
      if (xmlHttp.readyState == 4) {
       if (xmlHttp.status == 200) {
        try {
         var outMsg = xmlHttp.responseText;
         document.getElementById("updateArea").innerHTML = outMsg;
        } catch(e) {
         alert("There was a problem with loading the request: " + e.toString());
        }
       } else {alert("There was a problem with the request: " + xmlHttp.status);}
      } else {/* Ponawia ponownie żadanie */}
     };

    xmlHttp.send(null);
   } catch (e) {alert("Sorry, but I couldn't connecting server:\n" + e.toString());}
  }
}

window.onload = initAll;

function initAll() {
  document.getElementById("o_nas").onclick = getNewFile;
}

function getNewFile() {
  loadXmlHttpRequestObject(this.href);
  return false;
}


Chodzi o to by zmienną outMsg wyciągnąć jako globalną, czy jest to wogule możliwie? Problem leży przy "xmlHttp.onreadystatechange".
nevt
to ją zadeklaruj jako globalną, dodaj:
Kod
var outMsg;
gdzieś w ciele skryptu (nie w definicji funkcji, tylko poza) a wewnątrz onreadyststechange odwolaj się do niej bez var:
Kod
outMsg = xmlHttp.responseText;
Earth
Racja mój błędny post myślałem że ze zmienną źle zrobiłem. Bo dałem wykonanie 2 funkcji: jedna żądanie, a druga miała wyświetlać tą globalną zmienną. Teraz wiem, że po prostu 1 funkcja ściągała dane, a 2 wykonuje się już wtedy gdy ściąganie danych jeszcze się nie skończyło.

http://forum.php.pl/index.php?showtopic=87083

Temat do zamknięcia.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.