problems with DOM

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Bücker

    problems with DOM

    I want to save an object of an image in a variable.
    I am doing this with the internet explorer with no problems using the following line:
    oImage = node.childNodes (0).all["pic"]

    Now I wanted to use the W3C-standard and thought I could get my object using this here:
    oImage = node.childNodes (0).getElementB yId("pic")

    The node variable is from an xsl file:
    ....
    <div onclick="window .event.cancelBu bble = true; clickOnNode(thi s);"
    ....

    I don´t get it! I´ve tried a lot of variations and searched the net but I didn´t find anything.
    Thanks for every little hint on this topic! Greetings,
    Tim.


  • Martin Honnen

    #2
    Re: problems with DOM



    Tim Bücker wrote:
    [color=blue]
    > I want to save an object of an image in a variable.
    > I am doing this with the internet explorer with no problems using the following line:
    > oImage = node.childNodes (0).all["pic"]
    >
    > Now I wanted to use the W3C-standard and thought I could get my object using this here:
    > oImage = node.childNodes (0).getElementB yId("pic")[/color]

    Use
    node.ownerDocum ent.getElementB yId("pic")
    --

    Martin Honnen


    Comment

    • Tim Bücker

      #3
      Re: problems with DOM

      Thanks for the quick answer but unfortunately the problem is not solved.
      It is still the same - ignoring the active node always the first one in the
      document gets selected (I want to change icons for a menu whether they
      are selected or not)!

      Is more information needed? Any resources on the web concerning this
      topic? More ideas? I am getting more and more desperate...

      But thanks again for answering,
      Tim.

      "Martin Honnen" <mahotrash@yaho o.de> schrieb im Newsbeitrag news:3fc235a3$1 @olaf.komtel.ne t...[color=blue]
      >
      >
      > Tim Bücker wrote:
      >[color=green]
      > > I want to save an object of an image in a variable.
      > > I am doing this with the internet explorer with no problems using the following line:
      > > oImage = node.childNodes (0).all["pic"]
      > >
      > > Now I wanted to use the W3C-standard and thought I could get my object using this here:
      > > oImage = node.childNodes (0).getElementB yId("pic")[/color]
      >
      > Use
      > node.ownerDocum ent.getElementB yId("pic")
      > --
      >
      > Martin Honnen
      > http://JavaScript.FAQTs.com/
      >[/color]


      Comment

      • VK

        #4
        Re: problems with DOM

        "DIV via DOM" doesn't expose getElementById( ) neither getElementByNam e()
        methods, at least not in IE.

        If you really want to keep your life as complicated as possible ;-) then
        use something like:
        myNode.childNod es(0).getElemen tsByTagName('IM G')[0].src = newSrc;

        To spice it up, you actually can remove first the retrieved image node,
        create a new one with another src attribute and insert it back.

        Last stupid question: why not use the global document.images[i]
        collection? It's totally correct 3W-approved syntacs, if you are worried
        about it.



        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: problems with DOM

          "Tim Bücker" <timtos.NOSPAM@ web.de> writes:
          [color=blue]
          > I want to save an object of an image in a variable.[/color]
          [color=blue]
          > I am doing this with the internet explorer with no problems using
          > the following line:[/color]
          [color=blue]
          > oImage = node.childNodes (0).all["pic"][/color]

          The "all" property is IE specific. You could use:
          oImage = node.firstChild .getElementsByT agName("*")["pic"];
          The getElementsByTa gName("*") creates a collection equivalent to the
          "all" collection.

          I would just do
          oImage = document.images["pic"];
          If "pic" is the ID of the image, then it must be unique in the
          document.
          [color=blue]
          > Now I wanted to use the W3C-standard and thought I could get my object using this here:
          > oImage = node.childNodes (0).getElementB yId("pic")[/color]

          getElementById only exists on the document object. There is no need for
          more than one, since Id's must be unique in the document.

          If the node isn't from this document, then you can find the appropriate
          document as: node.ownerDocum ent
          (Same goes for document.images above).
          [color=blue]
          > The node variable is from an xsl file:
          > ...
          > <div onclick="window .event.cancelBu bble = true; clickOnNode(thi s);"[/color]

          "window.eve nt" is IE specific, and so is "cancelBubb le".
          In onclick attribute values, the variable "event" points directly to
          the event (in other browsers it is a local variable, in IE it is global).

          The W3C version would be:
          <div onclick="event. stopPropagation ();clickOnNode( this);">
          Since IE doesn't support that, you'll have to do both:
          <div onclick="if(eve nt.stopPropagat ion){event.stop Propagation();}
          else{event.canc elBubble = true;}; clickOnNode(thi s);">

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Tim Bücker

            #6
            Re: problems with DOM

            Thanks a lot for all the interesting information!
            I will work myself through each of your proposals and see if they work in my case.
            Unfortunately (for example) "pic" is not unique; as I use a xsl file like this:

            <xsl:template match="node">
            ....
            <img border="0" id="pic">
            <xsl:attribut e name="src">
            <xsl:value-of select="testPic " />
            </xsl:attribute>
            </img>
            ....
            </xsl:template>

            But anyway thanks to everyone who tried to help me!
            Thanks a lot.

            Greetings,
            Tim.


            Comment

            • Tim Bücker

              #7
              Re: problems with DOM

              Unfortunately I am always getting an error message in the following line:
              oImage = myNode.firstChi ld.getElementsB yTagName("*")["pic"];
              Although getElementsByTa gName returns an object. So here is nothing wrong I think.

              Event thread: onclick
              Error:
              name: TypeError
              message: Statement on line 71: Expression did not evaluate to a function object: myNode.firstChi ld.getElementsB yTagName

              Any ideas? Again everything is working great using the IE.

              Thanks again for answering,
              Tim.


              Comment

              • Tim Bücker

                #8
                Re: problems with DOM

                Using Opera getElementsByTa gName does NOT return an object!
                [ myNode.firstChi ld.getElementsB yTagName("*") ]

                "Tim Bücker" <timtos.NOSPAM@ web.de> schrieb im Newsbeitrag news:bpvs20$15s v$1@news.uni-koblenz.de...[color=blue]
                > Unfortunately I am always getting an error message in the following line:
                > oImage = myNode.firstChi ld.getElementsB yTagName("*")["pic"];
                > Although getElementsByTa gName returns an object. So here is nothing wrong I think.
                >
                > Event thread: onclick
                > Error:
                > name: TypeError
                > message: Statement on line 71: Expression did not evaluate to a function object: myNode.firstChi ld.getElementsB yTagName
                >
                > Any ideas? Again everything is working great using the IE.
                >
                > Thanks again for answering,
                > Tim.
                >
                >[/color]


                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: problems with DOM

                  "Tim Bücker" <timtos.NOSPAM@ web.de> writes:
                  [color=blue]
                  > Using Opera getElementsByTa gName does NOT return an object!
                  > [ myNode.firstChi ld.getElementsB yTagName("*") ][/color]

                  Are you sure the first child is not a text node?
                  Remember, IE removes empty (all whitespace) text nodes from the document
                  tree, Mozilla and Opera doesn't.

                  Drop the ".firstChil d", and it will probably work.

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Tim Bücker

                    #10
                    Re: problems with DOM

                    Thanks a lot! That was the magic answer - at least my menu is now working on IE _and_ Opera. Unfortunately Netscape is still a
                    problem...

                    If I am honest, I did not really understand the thing with the text nodes.
                    Do you perhaps know a good internet resource about that kind of cross-browser compatibility?

                    Again - thanks a lot!
                    Greetings,
                    Tim.

                    "Lasse Reichstein Nielsen" <lrn@hotpop.com > schrieb im Newsbeitrag news:znekz88i.f sf@hotpop.com.. .[color=blue]
                    > "Tim Bücker" <timtos.NOSPAM@ web.de> writes:
                    >[color=green]
                    > > Using Opera getElementsByTa gName does NOT return an object!
                    > > [ myNode.firstChi ld.getElementsB yTagName("*") ][/color]
                    >
                    > Are you sure the first child is not a text node?
                    > Remember, IE removes empty (all whitespace) text nodes from the document
                    > tree, Mozilla and Opera doesn't.
                    >
                    > Drop the ".firstChil d", and it will probably work.
                    >
                    > /L
                    > --
                    > Lasse Reichstein Nielsen - lrn@hotpop.com
                    > DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                    > 'Faith without judgement merely degrades the spirit divine.'[/color]


                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: problems with DOM

                      "Tim Bücker" <timtos.NOSPAM@ web.de> writes:

                      (Please don't top post)
                      [color=blue]
                      > Thanks a lot! That was the magic answer - at least my menu is now
                      > working on IE _and_ Opera. Unfortunately Netscape is still a
                      > problem...[/color]

                      Do tell, what is the problem :)
                      [color=blue]
                      > If I am honest, I did not really understand the thing with the text
                      > nodes. Do you perhaps know a good internet resource about that kind
                      > of cross-browser compatibility?[/color]

                      Not really, no, sorry.

                      About the text nodes:

                      The DOM (Document Object Model) uses different kinds of DOM "Nodes"
                      (objects with some basic properties and methods) to represent
                      different parts of a document. There are, among others, Document
                      Nodes, Comment Nodes, Element Nodes and Text Nodes. The different
                      types have more specialized properties and methods than the
                      abstract Node class.

                      The code
                      <div>foo<span>b ar</span>baz</div>
                      generates one Element Node for the div element. It has three child nodes:
                      - One text node containing the text "foo".
                      - One Element node for the span element. It has a text node as child,
                      containing "bar".
                      - One text node containing the text "baz".

                      The difference between browsers here is when two tags appear next to each other
                      with only whitespace between, e.g.:
                      <div>
                      <span>foo</span>
                      </div>

                      Between "<div>" and "<span>" is one newline and two spaces. In Mozilla
                      and Opera, that gives rise to a Text Node with those three characters.
                      IE doesn't generate Text Nodes for strings containing only whitespace.

                      Your problem was that getElementsByTa gName is a method of Element Nodes,
                      not Text Nodes, and the first child element of your node was a text node
                      in Mozilla/Opera and an element node in IE.

                      /L
                      --
                      Lasse Reichstein Nielsen - lrn@hotpop.com
                      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      • Tim Bücker

                        #12
                        Re: problems with DOM

                        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote:[color=blue]
                        > (Please don't top post)[/color]

                        Sorry for that!
                        [color=blue][color=green]
                        > > Thanks a lot! That was the magic answer - at least my menu is now
                        > > working on IE _and_ Opera. Unfortunately Netscape is still a
                        > > problem...[/color]
                        >
                        > Do tell, what is the problem :)[/color]

                        I think the problem is a bit hard to explain - that was the reason why I didn´t ask you.
                        But I´ll give it a try:

                        I am trying to develop a dynamic menu using xml for the data and xsl for the design.
                        As usual you have a + sign and a - sign in front of each node that has children.
                        Thanks to your help I have now managed to get it run on IE and Opera and the expansion
                        of nodes also works using Netscape 7 (the only version I am trying at the moment) the
                        BIG problem is that the collapsing is absolutely not working using NS! The main problem
                        at the moment is that clicking on a - sign not only all the children are disappearing but also
                        the parent node so that it is now possible to create a totally blank page clicking through the
                        menu.

                        My collapse function looks like this:

                        function collapse(myNode )
                        {
                        var i, oImage
                        myNode.getEleme ntsByTagName('* ')["pic"].src = myNode.getAttri bute("pictureCl osed");

                        for (i=0; i<myNode.childN odes.length; i++)
                        {
                        // to get rid of the empty text nodes
                        if (myNode.childNo des[i].style)
                        {
                        if (myNode.tagName == "DIV")
                        {
                        if (myNode.id != "root")
                        myNode.childNod es[i].style.display = "none"
                        collapse(myNode .childNodes[i])
                        }
                        }
                        }
                        myNode.setAttri bute("open", "false");

                        /* // This code here works using IE and Opera (debug version)
                        for (i=0; i<myNode.childN odes.length; i++)
                        {
                        if (myNode.childNo des(i).tagName == "DIV")
                        {
                        if (myNode.id != "root")
                        myNode.childNod es(i).style.dis play = "none"
                        collapse(myNode .childNodes(i))
                        }
                        }
                        */
                        }

                        I hope I could make myself clear at this time of day.
                        If you need more information I would love to send it to you ;-)
                        [color=blue]
                        > About the text nodes:[/color]
                        Thanks a lot for the information about text nodes!

                        Thank you for taking you time!
                        Greetings,
                        Tim.


                        Comment

                        Working...