onreadystatechange not firing in IE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • THEDuke141
    New Member
    • Sep 2009
    • 3

    #1

    onreadystatechange not firing in IE

    Here's a good one i can't figure out. I have an AJAX application running off a PHP script and it works fine in FF(3.6.3), Safari(4.0.5), BUT it won't work in IE(8.0.76)[either 32bit or 64bit], and i can't figure out why.

    By not working, i mean that my div's content (named: dyk) won't update in IE, but updates fine in other browsers when clicking on a link (though a JS onClick event).

    Here's my code so far:

    index.php
    Code:
    <?php
    ...
    echo "<script type='text/javascript' src='./includes/getInfo.inc.js'></script>";
    ...
    include("didyouknow.php");
    ...
    ?>

    didyouknow.php
    Code:
    <?php
    ...
    echo "<li><a href='#' onClick='getContent(\"didyouknow\", \"4\", \"dyk\")'>Frames</a></li>";
    ...
    ?>

    getInfo.inc.js
    Code:
    var httpObject = null;
    var adrs;
    
    // Get the HTTP Object
    function getHTTPObject(){	
      if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
      else if (window.XMLHttpRequest) return new XMLHttpRequest();
      else {
        alert("Your browser does not support AJAX.");
        return null;
      }
    } 
    
    // Change the value of the div
    function setOutput(eid){	
    	if(httpObject.readyState == 4){
        document.getElementById(eid).innerHTML = httpObject.responseText;
        alert("blah");
      }
    }
    
    // Implement logic
    function getContent(page, section, eid){
      httpObject = getHTTPObject();
      
      adrs=page+"_Info.php?s="+section;
      
      if (httpObject != null) {
        httpObject.open("GET", adrs, true);
        httpObject.send(null);
        httpObject.onreadystatechange = function(){setOutput(eid);};
      }
    }

    didyouknow_Info .php (simple code for testing)
    Code:
    <?php
      if($_GET){
        $s = $_GET['s'];
      }
      echo "<p class='lg'>";
      switch ($s) {
        case 1:
          echo "Section ".$s;
          break;
        case 2:
          echo "Section ".$s;
          break;
        case 3:
          echo "Section ".$s;
          break;
        default:
          echo "Section doesn't exist.";
      } 
      echo "</p>";
    ?>
    That's what i have so far.

    When executing the code, i get no JS errors or any other errors.

    With the help of JS alerts in my JS script, i've come to figure out that in IE, httpObject.onre adystatechange = function(){setO utput(eid);}; won't always fire when i click on a link (but the rest of the JS script works fine).

    Any help and thoughts are greatly appreciated!!

    Thank you!
    Duke
    Last edited by THEDuke141; Apr 21 '10, 12:39 AM. Reason: Question resolved.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    You should be setting the onreadystatecha nge property before calling the send function. - The onreadystatecha nge property holds the callback function used when the send function completes, so by setting the property after calling it you run the risk of the function completing before the property is set.

    It shouldn't be *likely* when you set it in the line right after the call, but it is a definite possibility, especially when dealing with broken browsers like IE.

    Comment

    • THEDuke141
      New Member
      • Sep 2009
      • 3

      #3
      Well i'll be damned. That totally fix it.

      I was going crazy after seeing a a million code snippets with the send function after the onreadystatecha nge property and not being able to figure out why it wasn't working.

      Thanks a million Atli!

      Comment

      Working...