SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Correct use of AJAX callback

    With the script below the result is undefined if set to asynchronous in the open() method but works if set to synchronous.

    What am I doing wrong?

    Code:
    var x, l, objX, returnedData, oldCSS, newCSS;
    
    returnedData = null;
    
    function getCSS(url, callback) {
    
        objX = new XMLHttpRequest();
    
        if (objX !== null) {
    
            objX.open("GET", url, false);
    
            objX.onreadystatechange = function () {
    
                //if readyState is not 4 or or status not 200 then there is a problem that needs attending
                if (objX.readyState === 4) {
                    if (objX.status === 200) {
    
                        returnedData = objX.responseText.toString();
                        callback(returnedData);
    
                    } else {
    
                        //alert('HTTP error ' + objX.status); // change to FALSE on live site
                    } // end if status === 200
                } // end if readstate === 4
            };
    
            objX.send();
    
        } else {
    
            alert("You do not have AJAX implemented on your browser, sorry.");
    
        } //CS.Json.objX
    }
    
    
    getCSS("http://www.ultrabox-two.andywalpole.me/styles/style.css", function (result) {
        alert(result);
    });
    Let everyday be Christmas

  2. #2
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it seems to be fine now for some reason
    Let everyday be Christmas


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •