Load xml in Javascript doesnt work in firefox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hush

    Load xml in Javascript doesnt work in firefox

    Hi,

    The following code works fine in IE7 but FF returns with an error:

    Access denied to achieve the property Element.firstCh ild. In this
    line:
    nodes = xmlDoc.document Element.childNo des;

    My code:

    function CheckNick()
    {
    nick = document.getEle mentById("nick" );
    nickName = nick.value;

    if (window.ActiveX Object)
    {
    xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
    xmlDoc.onreadys tatechange = function () {
    if (xmlDoc.readySt ate == 4) handleXML()
    };
    xmlDoc.load("is NickAvailable.p hp?nick="+nickN ame+"&IEHACK="+ (new
    Date()).getTime ());
    }
    else if (document.imple mentation &&
    document.implem entation.create Document)
    {
    xmlDoc = document.implem entation.create Document("", "", null);
    xmlDoc.onload = handleXML;
    xmlDoc.load("is NickAvailable.p hp?nick="+nickN ame+"&IEHACK="+ (new
    Date()).getTime ());
    }

    else
    {
    alert('Your browser can\'t handle this script');
    return;
    }

    }
    function handleXML()
    {
    nodes = xmlDoc.document Element.childNo des;
    alert(nodes.ite m(0).text);
    //
    alert(xmlDoc.ge tElementsByTagN ame('nick').chi ldNodes.firstCh ild.nodeValue);
    }


    Regards

    Hush
  • Martin Honnen

    #2
    Re: Load xml in Javascript doesnt work in firefox

    Hush wrote:
    The following code works fine in IE7 but FF returns with an error:
    >
    Access denied to achieve the property Element.firstCh ild. In this
    line:
    nodes = xmlDoc.document Element.childNo des;
    It is not likely that you get an error on 'firstChild' in an expression
    that does not contain that name.

    function handleXML()
    {
    nodes = xmlDoc.document Element.childNo des;
    alert(nodes.ite m(0).text);
    //
    alert(xmlDoc.ge tElementsByTagN ame('nick').chi ldNodes.firstCh ild.nodeValue);
    That expression does not make sense, getElementsByTa gName gives a
    collection which you can index, and childNodes gives a collection which
    you can index too.
    I think you should show us the XML you load and describe the elements
    you want to read out, then we can help coding the access to those elements.

    --

    Martin Honnen

    Comment

    • Hush

      #3
      Re: Load xml in Javascript doesnt work in firefox

      On 7 Aug., 14:41, Martin Honnen <mahotr...@yaho o.dewrote:
      Hush wrote:
      The following code works fine in IE7 but FF returns with an error:
      >
      Access denied to achieve the property Element.firstCh ild. In this
      line:
       nodes = xmlDoc.document Element.childNo des;
      >
      It is not likely that you get an error on 'firstChild' in an expression
      that does not contain that name.
      Sorry, I typed it wrong. Its of course Access denied to achieve the
      property Element.childNo des, and not firstNode.
      That expression does not make sense, getElementsByTa gName gives a
      collection which you can index, and childNodes gives a collection which
      you can index too.
      I think you should show us the XML you load and describe the elements
      you want to read out, then we can help coding the access to those elements.
      Ok. Actually I only want to extract 1 number (1 or 0) so I only need 1
      value from the XML. My XML is as follows:
      <?xml version="1.0" encoding="iso-8859-1"?>
      <nick>
      <isa>0</isa>
      </nick>

      I'm trying to get the value of the element <isa>

      Hush



      Comment

      • Martin Honnen

        #4
        Re: Load xml in Javascript doesnt work in firefox

        Hush wrote:
        Ok. Actually I only want to extract 1 number (1 or 0) so I only need 1
        value from the XML. My XML is as follows:
        <?xml version="1.0" encoding="iso-8859-1"?>
        <nick>
        <isa>0</isa>
        </nick>
        >
        I'm trying to get the value of the element <isa>
        var isaElements = xmlDoc.getEleme ntsByTagName('i sa');
        if (iseElements.le ngth 0)
        {
        var isa = isaElements[0];
        var n = isa.firstChild. nodeValue;
        }
        else
        {
        // handle case that 'isa' element was not found
        }

        That assumes that the 'isa' element always has some text in it.




        --

        Martin Honnen

        Comment

        • Hush

          #5
          Re: Load xml in Javascript doesnt work in firefox

          >
             var isaElements = xmlDoc.getEleme ntsByTagName('i sa');
             if (iseElements.le ngth 0)
             {
               var isa = isaElements[0];
               var n = isa.firstChild. nodeValue;
             }
             else
             {
               // handle case that 'isa' element was not found
             }
          I'm stil getting the error for firstChild in this line:
          var n = isa.firstChild. nodeValue;

          I tested in IE. And it worked nicely...

          Comment

          • Martin Honnen

            #6
            Re: Load xml in Javascript doesnt work in firefox

            Hush wrote:
            I'm stil getting the error for firstChild in this line:
            var n = isa.firstChild. nodeValue;
            Can you post the exact error message? Your first post said "access
            denied" which sounds more like a security exception. I am not sure why
            you would get that, if you tried to load the XML from a different origin
            then I would expect the load call to already throw an exception.
            If you have a URL we can visit then post that too.

            Does that problem occur with Firefox 3? Have you checked whether it also
            occurs with Firefox 2?

            --

            Martin Honnen

            Comment

            • Hush

              #7
              Re: Load xml in Javascript doesnt work in firefox

              On 7 Aug., 18:14, Martin Honnen <mahotr...@yaho o.dewrote:
              Hush wrote:
               I'm stil getting the error for firstChild in this line:
              var n = isa.firstChild. nodeValue;
              >
              Can you post the exact error message? Your first post said "access
              denied" which sounds more like a security exception. I am not sure why
              you would get that, if you tried to load the XML from a different origin
              then I would expect the load call to already throw an exception.
              If you have a URL we can visit then post that too.
              >
              Does that problem occur with Firefox 3? Have you checked whether it also
              occurs with Firefox 2?
              >
              --
              >
                      Martin Honnen
                     http://JavaScript.FAQTs.com/
              I translated the error from a danish version of firebug. I'm using
              FF3, but when I come to think of it I had a similar error a while ago
              with the same error. This was in an unchanged code, but after the
              upgrade to FF3, so this could definately be the cause. I havent tested
              in FF2. ( I will try that now)

              URL:
              Own this domain today. We make your shopping experience easy. Friendly and quick customer service.

              Comment

              • Hush

                #8
                Re: Load xml in Javascript doesnt work in firefox

                Does that problem occur with Firefox 3? Have you checked whether it also
                occurs with Firefox 2?
                It works in FF2. So the problem is in FF3 :|

                Is there a fix??

                Comment

                • Martin Honnen

                  #9
                  Re: Load xml in Javascript doesnt work in firefox

                  Hush wrote:
                  >>Does that problem occur with Firefox 3? Have you checked whether it also
                  >>occurs with Firefox 2?
                  >
                  It works in FF2. So the problem is in FF3 :|
                  Oddly enough I have just tested with Mozilla/5.0 (Windows; U; Windows NT
                  5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1 and entered some
                  characters into the "nick" field and I don't get any error in the error
                  console, instead each time an alert dialog with '1' is displayed.
                  Is there a fix??
                  I am still not sure why you get that error and I can't reproduce it.
                  As a workaround you might want to try to load the XML with
                  XMLHttpRequest instead of xmlDoc.load(), perhaps that avoids the problem.

                  --

                  Martin Honnen

                  Comment

                  • Thomas 'PointedEars' Lahn

                    #10
                    Re: Load xml in Javascript doesnt work in firefox

                    Hush wrote:
                    >>Does that problem occur with Firefox 3? Have you checked whether it also
                    >>occurs with Firefox 2?
                    >
                    It works in FF2. So the problem is in FF3 :|
                    True, but that does not necessarily mean that the problem *is* Fx 3.
                    For example, Firebug for Fx 3 is still beta.
                    Is there a fix??
                    For a start, you could try not to mix standards compliant and proprietary
                    features, which would be a good idea in any case.


                    PointedEars
                    --
                    Use any version of Microsoft Frontpage to create your site.
                    (This won't prevent people from viewing your source, but no one
                    will want to steal it.)
                    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

                    Comment

                    • Hush

                      #11
                      Re: Load xml in Javascript doesnt work in firefox

                      On 7 Aug., 19:10, Martin Honnen <mahotr...@yaho o.dewrote:
                      Hush wrote:
                      >Does that problem occur with Firefox 3? Have you checked whether it also
                      >occurs with Firefox 2?
                      >
                      It works in FF2. So the problem is in FF3 :|
                      >
                      Oddly enough I have just tested with Mozilla/5.0 (Windows; U; Windows NT
                      5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1 and entered some
                      characters into the "nick" field and I don't get any error in the error
                      console, instead each time an alert dialog with '1' is displayed.
                      >
                      An Alert should be displayed if it works.
                      The error message is in Firebug only, and its still a beta release.
                      But that still doesnt change the fact that it doesnt work in FF3...:(
                      I am still not sure why you get that error and I can't reproduce it.
                      As a workaround you might want to try to load the XML with
                      XMLHttpRequest instead of xmlDoc.load(), perhaps that avoids the problem.
                      >
                      I'm new in js, so can you give me an example?

                      Comment

                      • Hush

                        #12
                        Re: Load xml in Javascript doesnt work in firefox

                        True, but that does not necessarily mean that the problem *is* Fx 3.
                        For example, Firebug for Fx 3 is still beta.
                        >
                        Yes. You are right. But its still pretty annoying that there has to be
                        a difference:D
                        For a start, you could try not to mix standards compliant and proprietary
                        features, which would be a good idea in any case.
                        I'm sorry, I didnt understand a word of that..... (I'm new in JS)
                        Please bare with me....

                        Comment

                        • Martin Honnen

                          #13
                          Re: Load xml in Javascript doesnt work in firefox

                          Hush wrote:
                          An Alert should be displayed if it works.
                          The error message is in Firebug only, and its still a beta release.
                          But that still doesnt change the fact that it doesnt work in FF3...:(
                          Then disable Firebug and check whether it works then. I don't have
                          Firebug for FF 3 and it works for me.


                          --

                          Martin Honnen

                          Comment

                          • Hush

                            #14
                            Re: Load xml in Javascript doesnt work in firefox

                            Then disable Firebug and check whether it works then. I don't have
                            Firebug for FF 3 and it works for me.
                            Interesting... After I disabled it, the code goes to the 'else' in
                            this code:

                            if (isaElements.le ngth 0)
                            {

                            var isa = isaElements[0];
                            var n = isa.firstChild. nodeValue;

                            }
                            else
                            {
                            alert("isa element not found");// handle case that 'isa' element
                            was not found
                            }

                            Which has to mean that this line doesnt work (I guess):
                            var isaElements = xmlDoc.getEleme ntsByTagName('i sa');

                            Comment

                            • Hush

                              #15
                              Re: Load xml in Javascript doesnt work in firefox

                                 else
                                 {
                                   alert("isa element not found");// handle case that 'isa' element
                              was not found
                                 }
                              I commented the line above out, and now it seems to work :| I dont
                              understand..... ......

                              Own this domain today. We make your shopping experience easy. Friendly and quick customer service.


                              Does this make any sense to you?

                              Comment

                              Working...