How to parse XML in javascript for internet explorer

Discussion in 'JavaScript' started by rafiqasad, Sep 3, 2007.

  1. #1
    First file

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="javascript" type="text/javascript">
    var XMLHttpRequestObject = false;
    // for MS internet explorer
    try{
    XMLHttpRequestObject = new ActiveXObject("MSXML2.XMLHTTP");
    }catch(exception1){
    try{
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(exception2){
    XMLHttpRequestObject = false;
    }
    }

    //for fire fox btowser
    if(!XMLHttpRequestObject && window.XMLHttpRequest){
    XMLHttpRequestObject = new XMLHttpRequest();
    XMLHttpRequestObject.overrideMimeType("text/xml");
    }

    function getoption(scheme){
    var url = "options.php?scheme=" + scheme;
    if(XMLHttpRequestObject){
    XMLHttpRequestObject.open("GET",url);
    XMLHttpRequestObject.onreadystatechange = function(){
    if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status ==200){
    var xmlDocument = XMLHttpRequestObject.responseXML;
    options = xmlDocument.getElementsByTagName("option");
    listoptions();
    }
    }
    XMLHttpRequestObject.send(null);
    }else{
    var obj = document.getElementById(divID);
    obj.innerHTML = "Sorry Your browser does not support AJAX";
    }
    }
    function listoptions(){
    var loopIndex;
    var selectControl = document.getElementById('optionList');
    for(loopIndex = 0;loopIndex < options.length;loopIndex++){
    selectControl.options[loopIndex] = new Option(options[loopIndex].firstChild.data);
    }
    }
    function setoption(){
    document.getElementById('targetID').style.color = options[document.getElementById('optionList').selectedIndex].firstChild.data;
    }
    </script>
    </head>
    <body>
    <form>
    <select size="1" id="optionList" onchange="setoption();">
    <option>Select A scheme</option>
    </select>
    <input type="button" value="first scheme" onclick="getoption('1')"/>
    <input type="button" value="Second scheme" onclick="getoption('2')"/>
    </form>
    <div id="targetID">
    the color scheme comes here
    </div>
    </body>
    </html>

    Second file options.php
    
    <?
    if(isset($_GET["scheme"])){
    	header("Content-type:text/xml");
    	if($_GET["scheme"] == "1"){
    		$options = array('red','green','blue');	
    	}
    	if($_GET["scheme"] == "2"){
    		$options = array('black','white','orange');	
    	}
    	echo '<?xml version="1.0"?>';
    	echo '<options>';
    		foreach($options as $value){
    			echo '<option>';
    			echo $value;	
    			echo '</option>';
    		}
    	echo '</options>';
    }
    ?>
    
    PHP:
    This code works 100% for fire fox but not work in explorer. Plz some one guide help me
     
    rafiqasad, Sep 3, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    Please, post the bug/problem you're getting with IE.
     
    ajsa52, Sep 4, 2007 IP
  3. gadgetguru

    gadgetguru Banned

    Messages:
    160
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    can u tell me how to execute it........
    wher to enter this code to start workin.......
     
    gadgetguru, Sep 14, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Tell us which browser you are using, e.g. IE6 or IE7 (not IE in general) and provide more info rather than just "not work in explorer".

    I decided to try it and got the same results in Firefox 2, IE6 and IE7 - pressing the buttons changes the list items.

    gadgetguru - it is a web page, just copy paste the code into two .php pages.
     
    krt, Sep 14, 2007 IP