SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Not yet perfect mattalexx's Avatar
    Join Date
    Oct 2005
    Location
    Taos, NM, US
    Posts
    441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Bizarre (or maybe not) AJAX problem

    This function is being called from the onreadystatechange of an AJAX request object, and it works:
    Code:
    function updatePage(ID) {
       alert('Status has been changed to DENIED');
       if (request.readyState == 4 && request.status == 200) {
          var response = request.responseText;
          document.getElementById(\"status[\" + ID + \"]\").innerHTML = response;
       }
    }
    ...but this does not:
    Code:
    function updatePage(ID) {
       if (request.readyState == 4 && request.status == 200) {
          var response = request.responseText;
          document.getElementById(\"status[\" + ID + \"]\").innerHTML = response;
       }
    }
    In the second function, the if expression is false. How could an alert change this?
    Matt Alexander
    Alexander Site Design

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't explain it, maybe someone can, but I do know how to fix it.

    It's because the status property isn't available until readystate = 4, and somehow it's getting factored into the if.

    To fix it separate the ifs like this:

    Code:
    function updatePage(ID) {
    	if (request.readyState == 4) {
    		if(request.status == 200) {
    			var response = request.responseText;
    			document.getElementById(\"status[\" + ID + \"]\").innerHTML = response;
    		}
    	}
    }

  3. #3
    malloc
    Join Date
    Nov 2006
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This stinks of a timing issue. It definately shouldn't happen - && is a lazy operator which means that the left-hand-side is guaranteed to get evaluated first.

    Does this happen in all browsers?


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
  •