Ajax script not working w/ IE7+

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Aug 2009
Posts: 4
Reputation: KlNTARO is an unknown quantity at this point 
Solved Threads: 0
KlNTARO's Avatar
KlNTARO KlNTARO is offline Offline
Newbie Poster

Ajax script not working w/ IE7+

 
0
  #1
Aug 23rd, 2009
Greetings. I'm stumped with a particular problem involving an ajax script that works for Firefox, Opera, Safari, and Chrome... but unfortunately does not work with any version of IE (*actually I haven't tested IE6 or less yet, but who cares about those anymore*).

On the HTML side, a list of hospitals is generated, and when a hospital is selected, the content changes, based on the selection. In addition, the script also runs 'onload' to generate the initial list. The script is below, and the incomplete example can be seen at http://www.aahcp.com/nusurvey.php

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function narrowSelection(){
  2. xmlhttp=GetXmlHttpObject();
  3. if (xmlhttp==null){alert("Browser does not support HTTP Request");return;}
  4. var url="scripts/multiscript.php?narrowSelection="+document.getElementById('keyword').value;
  5. xmlhttp.onreadystatechange=stateChangedA;
  6. xmlhttp.open("GET",url,true);
  7. xmlhttp.send(null);}
  8.  
  9. function stateChangedA(){
  10. if (xmlhttp.readyState==4){document.getElementById("hospitallist").innerHTML=xmlhttp.responseText;}}
  11.  
  12. function GetXmlHttpObject(){
  13. if (window.XMLHttpRequest){/* code for IE7+, Firefox, Chrome, Opera, Safari */
  14. return new XMLHttpRequest();}
  15. if (window.ActiveXObject){/*code for IE6, IE5*/
  16. return new ActiveXObject("Microsoft.XMLHTTP");}return null;}
Thanks in advance for any assistance!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Ajax script not working w/ IE7+

 
0
  #2
Aug 23rd, 2009
Hi kintaro,

try this code:

  1. <script type="text/javascript">
  2. // <![CDATA[
  3. var d = document;
  4. var GetXmlHttpObject = 0;
  5. if ( d.getElementById ) {
  6. ( GetXmlHttpObject = function( ) {
  7. var req = 0;
  8. if ( req = new XMLHttpRequest() ) {
  9. return req;
  10. } try {
  11. var client = [ "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ];
  12. var cLen = client.length;
  13. for( var x = 0; x < cLen; x++ ) {
  14. if( req = new ActiveXObject( client[ x ] )) {
  15. return req;
  16. }
  17. }
  18. } catch( e ) {
  19. alert( "Browser does not support HTTP Request." );
  20. return 0;
  21. }
  22. } ); var stateChangedA = function() {
  23. d.getElementById("hospitallist").innerHTML = ( function( xhr ) {
  24. if(( { 4 : 4, complete : 4 }[ xhr.readyState ] ))
  25. return xhr.responseText;
  26. else
  27. return "HTTP Request failed!";
  28. } )( this );
  29. }
  30.  
  31. var narrowSelection = function() {
  32. var xmlHttp = GetXmlHttpObject();
  33. if ( xmlHttp ) {
  34. var url = "./script/multiscript.php?narrowSelection=" + d.getElementById("keyword").value;
  35. (( "overrideMimeType" in xmlHttp ) ? xmlHttp.overrideMimeType("text/xml") : xmlHttp );
  36. xmlHttp.onreadystatechange = stateChangedA;
  37. xmlHttp.open( "GET", encodeURIComponent( url ), true );
  38. xmlHttp.send( null );
  39. return;
  40. } void( 0 );
  41. }; window.onload = narrowSelection;
  42. }
  43.  
  44. // ]]>
  45. </script>
Last edited by essential; Aug 23rd, 2009 at 1:51 pm.
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 4
Reputation: KlNTARO is an unknown quantity at this point 
Solved Threads: 0
KlNTARO's Avatar
KlNTARO KlNTARO is offline Offline
Newbie Poster

Re: Ajax script not working w/ IE7+

 
0
  #3
Aug 23rd, 2009
I will give that a try. I may have to tweak it a bit for the server-side.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 4
Reputation: KlNTARO is an unknown quantity at this point 
Solved Threads: 0
KlNTARO's Avatar
KlNTARO KlNTARO is offline Offline
Newbie Poster

Re: Ajax script not working w/ IE7+

 
0
  #4
Aug 23rd, 2009
No good :-( It also stopped working for the other browsers, too. Thanks for trying, though!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 4
Reputation: KlNTARO is an unknown quantity at this point 
Solved Threads: 0
KlNTARO's Avatar
KlNTARO KlNTARO is offline Offline
Newbie Poster

Re: Ajax script not working w/ IE7+

 
0
  #5
Aug 23rd, 2009
I think I need to also mention that this server language is php, not asp.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Ajax script not working w/ IE7+

 
0
  #6
Aug 23rd, 2009
Hi,

this should be able to work under rough condition:
  1. <?xml version="1.0" encoding="utf-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html id="xhtml10S" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head>
  6. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <meta http-equiv="Content-Style-Type" content="text/css" />
  9. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  10. <title>www.daniweb.com</title>
  11. <script type="text/javascript">
  12. // <![CDATA[
  13.  
  14. var GetXmlHttpObject = function() {
  15. var xml = 0;
  16. try {
  17. if ( "XMLHttpRequest" in window ) {
  18. xml = new XMLHttpRequest();
  19. } else if ( "ActiveXObject" in window ) {
  20. var client = [ "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ];
  21. try {
  22. for ( var x = 0; x < client.length; x++ ) {
  23. if ( new ActiveXObject( client[ x ] )) {
  24. xml = new ActiveXObject( client[ x ] );
  25. break;
  26. }
  27. }
  28. } catch( e1 ) { xml = 0;
  29. }
  30. }
  31. } catch( e ) {
  32. if ( "createRequest" in window ) {
  33. xml = window.createRequest();
  34. }
  35. } return xml;
  36. };
  37.  
  38. var narrowSelection = function() {
  39. var xmlHttp = GetXmlHttpObject();
  40. if ( xmlHttp ) {
  41. var keywords = document.getElementById( "keyword" ) || keyword;
  42. var url = "script/multiscript.php?narrowSelection=" + encodeURIComponent( keywords.value );
  43. (( "overrideMimeType" in xmlHttp ) ? xmlHttp.overrideMimeType("text/xml") : xmlHttp );
  44. xmlHttp.onreadystatechange = function() {
  45. var hospital = document.getElementById( "hospitallist" ) || hospitallist;
  46. if ( { 4 : 4, complete : 4 }[ this.readyState ] && ( { 0 : "offline", 200 : "online" }[ this.status ] )) {
  47.  
  48. hospital.innerHTML = this.responseText;
  49.  
  50. }
  51. };
  52. xmlHttp.open( "GET", url, true );
  53. xmlHttp.send( null );
  54. return;
  55. } alert( "Your Browser does not support HTTP Request!" );
  56. };
  57. // ]]>
  58. </script>
  59. </head>
  60. <body>
  61. <noscript>
  62. <p>This site requires a browser that support <b>JavaScript</b>. </p>
  63. </noscript>
  64. <div>
  65. <div style="margin-bottom : 1em;" id="hospitallist"></div>
  66. Enter Keyword: <input type="text" id="keyword" name="keyword" value="" size="30" onchange="narrowSelection()" />
  67. </div>
  68. </body>
  69. </html>
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 26
Reputation: farhan386 is an unknown quantity at this point 
Solved Threads: 3
farhan386's Avatar
farhan386 farhan386 is offline Offline
Light Poster

Re: Ajax script not working w/ IE7+

 
0
  #7
Aug 26th, 2009
You may want to user javascript library such jQuery. jQuery is able to make ajax call, or many more javascript stuff for all major browser with a single code. Write less, do more
I'm spammer...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC