SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ActiveXObject not working in IE

    Hi all

    I am currently trying to create an XML document, in IE, like so

    Code:
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = 'false';
      xmlDoc.loadXML(myXmlString);
    But its not working.
    If I alert the value for xmlDoc, its nothing.
    I looked around the net and found this code on the w3 schools site

    http://www.w3schools.com/Dom/tryit.a...ry_dom_parser2

    and it works fine.
    If I put an alert after the first like, it displays "object".

    Does anyone have any suggestions for me?
    Thanks

  2. #2
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Have a look at the code below, working for me in both IE7, 8 and Firefox 2, 3.

    Code JavaScript:
     
    function(namespaceURI,  documentType){
    	var xmlDocument = null;
    	namespaceURI = namespaceURI || '';
    	documentType = documentType || null;
    	if (document.implementation && document.implementation.createDocument) {
    		xmlDocument = document.implementation.createDocument(
    			"",
    			"",
    			documentType
    		);
    	}
    	else if (typeof ActiveXObject != 'undefined') {
    		try {
    			xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
    		}
    		catch (e) { 
    		}
    	}
    }

  3. #3
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool thanks for your code, ill have a look at it.
    I did how ever figure out my "problem"
    After creating my xmlDoc variable
    I alerted it to check if it was there and was an object, like so

    Code:
      alert('place 1 - ' + xmlDoc);
    For some reason (only in IE...) if the xmlDoc variable isn't the only thing in the alert, it displays nothing. So when I tried

    Code:
      alert(xmlDoc);
    It displayed "[object]"

    Very very very strange
    Can anyone shed some light?

  4. #4
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you are loading an XML file using ActiveXObject in IE, You have to look for the readystate as in the case of an Ajax request.

  5. #5
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In my case (well my current case), i'm not actually using a file, im inputting a string. So would I still have to check for the readystate?

  6. #6
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Code below is working for me...
    Code JavaScript:
    var txt = '<Person><Name>ArunB</Name><Age>22</Age></Person>';
    if(document.all){
    	var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    	xmlDoc.async="false";
    	xmlDoc.loadXML(txt);
    }
    else{
    	var parser=new DOMParser();
    	var xmlDoc=parser.parseFromString(txt,"text/xml");
    }
     
    $("Person", xmlDoc).each(function(){
    	alert($(this).find("Name").text());
    });

  7. #7
    SitePoint Addict
    Join Date
    Jun 2006
    Location
    Durban, South Africa
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you getting exactly what im saying.
    My code is working, but the messages that you get when alerting you XML object are strange.

    Run this code

    Code:
        var txt = '<Person><Name>ArunB</Name><Age>22</Age></Person>';
        if(document.all) {
          var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
          xmlDoc.async="false";
          xmlDoc.loadXML(txt);
        } else {
          var parser=new DOMParser();
          var xmlDoc=parser.parseFromString(txt,"text/xml");
        }
     
        alert(xmlDoc);
        alert('1 - ' + xmlDoc);
    Check the results you get from the alerts (in IE). Its really strange...

    Just on another note. I tried running your code, and got an error
    "$ is not defined"
    I haven't seen that syntax before, what exactly is $?

  8. #8
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $ is jQuery.

    ---

    I haven't yet come upon such behavior in IE, it is quite strange really.

    Code javascript:
    alert((xmlDoc + "") == undefined);

    This will alert true.

    Code javascript:
    alert(xmlDoc == undefined);

    This will alert false.
    mmj


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •