By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,453 Members | 961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

onreadystatechange not firing in IE

P: 3
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
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ...
  3. echo "<script type='text/javascript' src='./includes/getInfo.inc.js'></script>";
  4. ...
  5. include("didyouknow.php");
  6. ...
  7. ?>

didyouknow.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. ...
  3. echo "<li><a href='#' onClick='getContent(\"didyouknow\", \"4\", \"dyk\")'>Frames</a></li>";
  4. ...
  5. ?>

getInfo.inc.js
Expand|Select|Wrap|Line Numbers
  1. var httpObject = null;
  2. var adrs;
  3.  
  4. // Get the HTTP Object
  5. function getHTTPObject(){    
  6.   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
  7.   else if (window.XMLHttpRequest) return new XMLHttpRequest();
  8.   else {
  9.     alert("Your browser does not support AJAX.");
  10.     return null;
  11.   }
  12.  
  13. // Change the value of the div
  14. function setOutput(eid){    
  15.     if(httpObject.readyState == 4){
  16.     document.getElementById(eid).innerHTML = httpObject.responseText;
  17.     alert("blah");
  18.   }
  19. }
  20.  
  21. // Implement logic
  22. function getContent(page, section, eid){
  23.   httpObject = getHTTPObject();
  24.  
  25.   adrs=page+"_Info.php?s="+section;
  26.  
  27.   if (httpObject != null) {
  28.     httpObject.open("GET", adrs, true);
  29.     httpObject.send(null);
  30.     httpObject.onreadystatechange = function(){setOutput(eid);};
  31.   }
  32. }

didyouknow_Info.php (simple code for testing)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   if($_GET){
  3.     $s = $_GET['s'];
  4.   }
  5.   echo "<p class='lg'>";
  6.   switch ($s) {
  7.     case 1:
  8.       echo "Section ".$s;
  9.       break;
  10.     case 2:
  11.       echo "Section ".$s;
  12.       break;
  13.     case 3:
  14.       echo "Section ".$s;
  15.       break;
  16.     default:
  17.       echo "Section doesn't exist.";
  18.   } 
  19.   echo "</p>";
  20. ?>
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.onreadystatechange = function(){setOutput(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
Apr 20 '10 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
Hey.

You should be setting the onreadystatechange property before calling the send function. - The onreadystatechange 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.
Apr 20 '10 #2

P: 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 onreadystatechange property and not being able to figure out why it wasn't working.

Thanks a million Atli!
Apr 21 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.