943,553 Members | Top Members by Rank

Ad:
Aug 23rd, 2009
0

Ajax script not working w/ IE7+

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
KlNTARO is offline Offline
4 posts
since Aug 2009
Aug 23rd, 2009
0

Re: Ajax script not working w/ IE7+

Hi kintaro,

try this code:

javascript Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Aug 23rd, 2009
0

Re: Ajax script not working w/ IE7+

I will give that a try. I may have to tweak it a bit for the server-side.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
KlNTARO is offline Offline
4 posts
since Aug 2009
Aug 23rd, 2009
0

Re: Ajax script not working w/ IE7+

No good :-( It also stopped working for the other browsers, too. Thanks for trying, though!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
KlNTARO is offline Offline
4 posts
since Aug 2009
Aug 23rd, 2009
0

Re: Ajax script not working w/ IE7+

I think I need to also mention that this server language is php, not asp.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
KlNTARO is offline Offline
4 posts
since Aug 2009
Aug 23rd, 2009
0

Re: Ajax script not working w/ IE7+

Hi,

this should be able to work under rough condition:
javascript Syntax (Toggle Plain Text)
  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>
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Aug 26th, 2009
0

Re: Ajax script not working w/ IE7+

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
Reputation Points: 10
Solved Threads: 7
Light Poster
farhan386 is offline Offline
44 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: I got this code for email validation . I know its simple i a newbie , explanation ?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Need a DIV always at bootm by my way!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC