473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
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
2 4381
Atli
5,058 Expert 4TB
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
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

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

Similar topics

6
by: Krzysztof Kubiak | last post by:
I'm trying to make use of XMLHTTP object, but I've come across a problem. It seems that there is no way to create dynamic array of such XMLHTTP objects (to make several requests) and handle them...
7
by: Jeremy Gollehon | last post by:
Here is a simplified example of what I'm trying to understand. This works fine: --------------------------------------------------------------------------- window.open(sURL1,'Window')...
1
by: akhil.patel | last post by:
I realize that this may have been asked before, but I could not find a definitive answer. function loadXMLDoc(url, MyFunction) { // use native XMLHttpRequest object if (window.XMLHttpRequest)...
2
by: jhullu | last post by:
hello, I'm writing a program using XMLHttpRequest that works in the main case on IE and mozilla but this code works only on IE ... why ? probleme is located at mark 'HERE' at the end of code....
2
by: petermichaux | last post by:
Hi, I thought it is about time I tried writing some JavaScript with XMLHttpRequest instead of just using the Yahoo! UI library. The simple page below works in both Safari and Opera but I don't...
1
by: weston | last post by:
So, the following apparently works as a method of grafting a method onto an object (using the squarefree JS shell): obj = { x: 1, inc: null } result obj.inc = function () { this.x++ } result...
2
by: Peter Michaux | last post by:
Hi, I am trying out the Ajax timeout example in Flanagan's book p492. It works fine in Opera but in Firefox the the call to request.abort() doesn't stop onreadystatechange being called. I found...
10
by: HugeBob | last post by:
Hi All, I'm having a problem with a web app I'm writing. I have a form for which I'm going to validate one of the fields. I use AJAX to retrieve XML containing a list of valid entries. My app...
5
by: Heofz | last post by:
Hey all, I've been banging my head against a brick wall on this one for the last 12 hours, and the time has come for me to give up and consult the gurus on this one. The below URL contains a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.