piątek, 10 lutego 2012

Pobranie treści strony przy pomocy XMLHttpRequest

Poniższa funkcja pozwala pobrać treść podstrony przy pomocy JavaScript.
var get = function(url, fn){
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url);
  xhr.onabort = xhr.onerror = function(){
    fn(false);
  };
  xhr.onreadystatechange = function(resp){
    if( xhr.readyState === 4 ){
      fn(resp.target.responseText, resp);
    }
  };
  xhr.send();
};

Brak komentarzy:

Prześlij komentarz