AJAX IE7 Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RaefKandeel
    New Member
    • Jan 2008
    • 4

    AJAX IE7 Problem

    Hi all, this is my first post.

    I have a little ajax assignment that works fine on mozilla firefox 2.0, but doesn't work at all on Internet Explorer 7. Apparently it has to do with the line where tempDiv element takes the value from xhr.responseTex t. The line that says: tempDiv.innerHT ML= xhr.responseTex t;. Apparently Internet Explorer 7 cuts off some tags. There goes my code:-

    HTML:-
    [HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>Addres s Book</title>
    <link rel="stylesheet " rev="stylesheet " href="StyleShee t.css" />
    <script src="addressScr ipt.js" language=javasc ript></script>
    </head>
    <body>
    <h1>Address Book</h1>
    <h2>By: Raef Kandeel</h2>
    <b><font color=green>Ref erences: Visual QuickStart Guide JavaScript and Ajax for the Web, Sixth Edition, http://safari.oreilly. com/9781593271060/Iassignment_id1 4</font></b><br />
    <table><td>
    <select id="names" size=15></select></td>
    <td><div><b><fo nt color=red>Click the Name to Display the Contact Info</font></b></div></td></table>
    <p></p>
    </body>
    </html>
    [/HTML]

    Javascript:-
    Code:
    window.onload= initializeAJAX;
    var xhr=false;
    
    function initializeAJAX() {
         if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
         }
         else {
            if (window.ActiveXObject) {
               try {
                  xhr =new ActiveXObject("Msxml2.XMLHTTP");
               }
               catch (e) {xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
            }
         }
         if (xhr) {
            xhr.onreadystatechange = populateCombo;
            xhr.open("GET", "./AddressBook.xml", true);
            xhr.send(null);
         }
         else {
            alert("Sorry, but I couldn't create an XMLHttpRequest");
         }
    
    }
    
    function populateCombo () {
            //create new temporary element
            var tempDiv= createTempDiv();
    	    var contacts=tempDiv.getElementsByTagName ("name");  
        	var combo=document.getElementById ("names");
        	for (var i=0; i<contacts.length; i++) {
    		        var newContact = document.createElement('option');
    		        newContact.text = contacts[i].innerHTML; 
    		        try
                        {
                            combo.add(newContact,null); // standards compliant
                        }
                     catch(ex)
                        {      
                              combo.add(newContact); // IE only
                        }
           }
           combo.onchange=displayDetails;
    }
    
    function displayDetails () {
        var tempDiv= createTempDiv();
        var contacts=tempDiv.getElementsByTagName ("*");
        var divElement = document.getElementsByTagName ("div")[0];
        var neededContact= this.options [this.selectedIndex].text;
       	
       	//store elements in tag "p"
       	storeP (contacts);
       	
       	var i=1;
       
        while (document.getElementById ("name" +i)){
       	    var name=document.getElementById ("name" +i).innerHTML;
       	    var emailAddress=document.getElementById ("emailAddress" +i).innerHTML;
       	    var phone=document.getElementById ("phone" +i).innerHTML;
       	    var address=document.getElementById ("address" +i).innerHTML;
       	    if (name==neededContact) {
       	        var stringOutput="<b>Name:&nbsp;</b>";
       	        stringOutput = stringOutput + name + "<br>";
       	        stringOutput+="<b>Phone:&nbsp;</b>";
       	        stringOutput = stringOutput + phone + "<br>";
       	        stringOutput+="<b>Address:&nbsp;</b>";
       	        stringOutput = stringOutput + address + "<br>";
       	        stringOutput+="<b>E-mail Address:&nbsp;</b>";
       	        stringOutput = stringOutput + emailAddress + "<br>";    
       	        divElement.innerHTML= stringOutput;
    
       	    }
       	    i++;
       	}
    }
    
    function createTempDiv() {
               
            var tempDiv = document.createElement ("div");
            if (xhr.readyState == 4) {
                tempDiv.innerHTML= xhr.responseText;
            } 
            
                
            return tempDiv;  
    }
    
    
    
    function storeP (contacts) {
        var paragraphNode=document.getElementsByTagName ("p")[0];
        if (!paragraphNode.innerHTML) { 
            for (var i=0; i<contacts.length; i++) {
                document.getElementsByTagName ("p")[0].appendChild ((contacts[i]).cloneNode(true));
        }}
    }
    CSS:-
    Code:
    h1
    {
        text-align:center;
    }
    
    select 
    {
       color:blue;
    }
    
    p
    {
        display:none;
    }
    
    td
    {
        vertical-align:top;
     }
    
    table 
    {
        border:15px;
        border-style:solid;
        border-spacing:100px;
    }
    
    div
    {
        width:700px;    
    }
    XML:-

    [HTML]<?xml version="1.0" encoding="utf-8" ?>

    <addressBook>
    <person>
    <name id="name1">Rae f Kandeel</name>
    <address id="address1">o laya street</address>
    <phone id="phone1">321 54451</phone>
    <emailAddress id="emailAddres s1">****@hotmai l.com</emailAddress>
    </person>
    <person>
    <name id="name2">Moha mmed Hassanein Amer</name>
    <address id="address2">T a7'asosy Street</address>
    <phone id="phone2">645 61215</phone>
    <emailAddress id="emailAddres s2">****@gmail. com</emailAddress>
    </person>
    <person>
    <name id="name3">Moha mmed Edoso2y</name>
    <address id="address3">s hare3 enasr, egypt</address>
    <phone id="phone3">016 2545265</phone>
    <emailAddress id="emailAddres s3">****@gmail. com</emailAddress>
    </person>
    <person>
    <name id="name4">Esha 3rany Ayoob</name>
    <address id="address4">e l ma2atam</address>
    <phone id="phone4">016 66688822</phone>
    <emailAddress id="emailAddres s4">****@gmail. com</emailAddress>
    </person>
    </addressBook>[/HTML]

    Thanks Guys
    Last edited by acoder; Jan 14 '08, 09:11 AM. Reason: removed email addresses
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Hi Raef, welcome to TSDN!

    In populateCombo() , you've not checked that the readyState is 4 and the response is OK before assuming the data is ready to use.

    PS. I've removed the email addresses from the XML code in case they were real email addresses. This is for your own safety and that of others.

    Comment

    • RaefKandeel
      New Member
      • Jan 2008
      • 4

      #3
      Please note that the first statement in populateCombo calls createTempDiv which in turn checks the readyState. Please also note that it works fine in firefox 2.0, my problem is with IE7.

      Also please note that xhr.responseTex t value is fine in the explorer. tempDiv.innerHT ML, however is wrong. So, it is not a problem with ajax.

      Thanks for removing the e-mail addresses.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The div element hasn't been added to the body. That may be the problem as far as I can see. If it's a valid XML file, you could use responseXML instead and use getElementsByTa gName on the responseXML object.

        Comment

        • Dasty
          Recognized Expert New Member
          • Nov 2007
          • 101

          #5
          You can not use innerHTML to insert XML tags into your site, even when it is specified as xhml. innerHTML just simply cant do it right. Well, firefox fixed it some time ago, but it's just FF (it is from ver 1.5 or so). But it is not problem in your case, because you are using it just as "workaround " for XML parsing. As acoder said, use responseXML property to parse XML.

          PS: One more thing about innerHTML: from my experience, dont try to read (find) elements created by setting innerHTML in the same javascript "thread" (as you did) :). It MAY fail ...

          Comment

          • RaefKandeel
            New Member
            • Jan 2008
            • 4

            #6
            Thanks for your help. I am facing problems with responseXML. When i try:-

            Code:
            alert (xhr.responseXML.getElementsByTagName("name")[3].nodeValue);
            I get null. I was expecting to get instead: <name id="name4">Esha 3rany Ayoob</name>

            However when I try: -

            Code:
            alert (xhr.responseXML.getElementsByTagName("name")[3].nodeName);
            I get name, which shows it knows what I am talking about.

            Edited: Okay I figured it out.

            Comment

            • RaefKandeel
              New Member
              • Jan 2008
              • 4

              #7
              Hi guys. Thanks to you, my code worked on both Mozilla Firefox 2.0 and Internet Explorer 7. I need little help of you. I am going to post my code again and please tell me if it adheres to the best programming practices. Any improvement ideas to the code would be highly appreciated.

              [HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
              <html xmlns="http://www.w3.org/1999/xhtml" >
              <head>
              <title>Addres s Book</title>
              <link rel="stylesheet " rev="stylesheet " href="StyleShee t.css" />
              <script src="addressScr ipt.js" language=javasc ript></script>
              </head>
              <body>
              <center><a href="#"><img src="images/address.jpg" width="500" height="100" id="clickLink"/></a></center>
              <h2>By: Raef Kandeel</h2>
              <b><font color=green><i> &nbsp; &nbsp; &nbsp; &nbsp;Reference s: Visual QuickStart Guide JavaScript and Ajax for the Web, Sixth Edition,&nbsp;& nbsp;&nbsp;&nbs p; http://safari.oreilly. com/9781593271060/Iassignment_id1 4</i></font></b><br />
              <table align=center><t d>
              <select id="names" size=15></select>&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;&nbsp;&nbsp; </td>
              <td><div><b><fo nt color=red>the Name to Display the Contact Info</font></b></div></td></table>
              </body>
              </html>[/HTML]



              [HTML]
              <?xml version="1.0" encoding="utf-8" ?>
              <addressBook>
              <person>
              <name>Raef Kandeel</name>
              <address>olay a street</address>
              <phone>32154451 </phone>
              <emailAddress>r ******@gmail.co m</emailAddress>
              </person>
              <person>
              <name>Mohamme d Hassanein Amer</name>
              <address>Ta7'as osy Street</address>
              <phone>64561215 </phone>
              <emailAddress>* *****@gmail.com </emailAddress>
              </person>
              <person>
              <name>Mohamme d Edoso2y</name>
              <address>shar e3 enasr, egypt</address>
              <phone>01625452 65</phone>
              <emailAddress>* *****@gmail.com </emailAddress>
              </person>
              <person>
              <name>Esha3ra ny Ayoob</name>
              <address>el ma2atam</address>
              <phone>01666688 822</phone>
              <emailAddress>* *****@gmail.com </emailAddress>
              </person>
              <person>
              <name>3amr Med7at</name>
              <address>3amr Med7at's address is not available</address>
              <phone>255562 4</phone>
              <emailAddress>* ******@gmail.co m</emailAddress>
              </person>
              </addressBook>[/HTML]

              Code:
              window.onload= initializeAJAXandLink;
              var xhr=false;
              
              function initializeAJAXandLink() {
                   if (window.XMLHttpRequest) {
                      xhr = new XMLHttpRequest();
                   }
                   else {
                      if (window.ActiveXObject) {
                         try {
                            xhr =new ActiveXObject("Msxml2.XMLHTTP");
                         }
                         catch (e) {xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
                      }
                   }
                   if (xhr) {
                      xhr.onreadystatechange = populateCombo;
                      xhr.open("GET", "./AddressBook.xml", true);
                      xhr.send(null);
                   }
                   else {
                      alert("Sorry, but I couldn't create an XMLHttpRequest");
                   }
                   
                   //also initialize the link
                  document.getElementById ("clickLink").onmouseover= changeLinkColor;
                  document.getElementById ("clickLink").onmouseout= changeBackLinkColor;
                  document.getElementById ("clickLink").onmousedown= openLinkWindow;   
              }
              
              function populateCombo () {
                      var tempDiv= createTempDiv();
              	    var contacts=tempDiv.getElementsByTagName ("name"); 
                  	var combo=document.getElementById ("names");
                  	for (var i=0; i<contacts.length; i++) {
              		        var newContact = document.createElement('option');
              		        newContact.text = contacts[i].firstChild.nodeValue; 
              		        try
                                  {
                                      combo.add(newContact,null); // standards compliant
                                  }
                               catch(ex)
                                  {      
                                        combo.add(newContact); // IE only
                                  }
                     }
                     combo.onchange=displayDetails;
              }
              
              function displayDetails () {
                  var tempDiv= createTempDiv();
                  var contacts=tempDiv.getElementsByTagName ("person");
                  var divElement = document.getElementsByTagName ("div")[0];
                  var neededContact= this.options [this.selectedIndex].text;
                	for (var i=0; i<contacts.length; i++) {
                 	   	var name=tempDiv.getElementsByTagName ("name")[i].firstChild.nodeValue;
                 	    var emailAddress=tempDiv.getElementsByTagName ("emailAddress")[i].firstChild.nodeValue;
                 	    var phone=tempDiv.getElementsByTagName ("phone")[i].firstChild.nodeValue;
                 	    var address=tempDiv.getElementsByTagName ("address")[i].firstChild.nodeValue;
                 	    if (name==neededContact) {
                 	        var stringOutput="<b>Name:&nbsp;</b>";
                 	        stringOutput = stringOutput + name + "<br>";
                 	        stringOutput+="<b>Phone:&nbsp;</b>";
                 	        stringOutput = stringOutput + phone + "<br>";
                 	        stringOutput+="<b>Address:&nbsp;</b>";
                 	        stringOutput = stringOutput + address + "<br>";
                 	        stringOutput+="<b>E-mail Address:&nbsp;</b>";
                 	        stringOutput = stringOutput + emailAddress + "<br>";    
                 	        divElement.innerHTML= stringOutput;
              
                 	    }
                 	}
              }
              
              function createTempDiv() {
                         
                      var tempDiv = false;
                      if (xhr.readyState == 4) {
                          if (xhr.status == 200) {
                              tempDiv= xhr.responseXML;  
                          }                         
                      } 
                      return tempDiv;  
              }
              
              function changeLinkColor () {
                  this.src = "images/address_on.jpg";
              }
              
              function changeBackLinkColor() {
                    this.src = "images/address.jpg";
              }
              
              function openLinkWindow() {
                   var newWindow = window.open("http://safari.oreilly.com/9781593271060/Iassignment_id14", "catWin", "resizable=yes, width=350,height=260");
                   return false;
              
              }
              [HTML]
              select
              {
              color:blue;
              }

              td
              {
              vertical-align:top;


              }

              table
              {
              background-position:center ;
              border:7px;
              border-style:solid;
              border-color:Black;
              }

              div
              {
              width:350px;
              }
              [/HTML]

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by RaefKandeel
                Hi guys. Thanks to you, my code worked on both Mozilla Firefox 2.0 and Internet Explorer 7. I need little help of you. I am going to post my code again and please tell me if it adheres to the best programming practices. Any improvement ideas to the code would be highly appreciated.
                You should use a strict doctype where possible and validate the HTML/CSS using the W3C validator.

                The language attribute of the script tag has been deprecated - use type="text/javascript" instead. The font and center tags are also deprecated - use CSS instead. The table has no tr tag - you should discover that if you validate. The link should point to a real URL.

                The populateCombo function will be called every time the readyState changes so you should wrap the whole function content in an if statement checking for ready state 4. The reason for this is that the code unnecessarily accesses the select element and sets its onchange.

                Comment

                Working...