Need help with insertBefore

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

    #1

    Need help with insertBefore

    Tonight I started writing a script that places a "Copy All" link
    before every PRE tag. The link would copy the code in the pre tag. I
    can't get this script to work. Can anyone help me?
    Here is my script:
    function makeIt() {
    var where = document.getEle mentsByTagName( "TABLE")[4].childNodes[0].childNodes[0].childNodes[0].childNodes;
    var base = document.getEle mentsByTagName( "PRE")[0];
    var link = document.create Element("SPAN") ;
    var text = document.create TextNode("Copy All");
    link.appendChil d(text);
    for (var i=0;i<where.len gth;i++) {
    if (where[i].nodeName == 'PRE'))
    document.body.i nsertBefore(lin k,where[i]);
    }
    }

    Thank you so much, Dante.
  • Martin Honnen

    #2
    Re: Need help with insertBefore



    Dante wrote:
    [color=blue]
    > Tonight I started writing a script that places a "Copy All" link
    > before every PRE tag. The link would copy the code in the pre tag. I
    > can't get this script to work. Can anyone help me?
    > Here is my script:
    > function makeIt() {
    > var where = document.getEle mentsByTagName( "TABLE")[4].childNodes[0].childNodes[0].childNodes[0].childNodes;[/color]

    The problem might be all those childNodes references, don't make
    assumptions about childNodes being (particular) element nodes, there are
    text nodes in the DOM of Mozilla and Opera for instance, and there are
    <tbody> element nodes included by the SGML/HTML parser for instance.
    If you want to insert into certain elements then use
    element.getElem entsByTagName
    or loop through childNodes but make sure that you check for nodeType and
    tagName.





    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Need help with insertBefore

      pianoman@reno.c om (Dante) writes:
      [color=blue]
      > Tonight I started writing a script that places a "Copy All" link
      > before every PRE tag. The link would copy the code in the pre tag. I
      > can't get this script to work.[/color]

      If you tell us what goes wrong, we won't have to guess.
      The three questions one should answer before asking for help in debugging:

      - What do you do (the full code, which browser, how to reproduce the
      bug - i.e., which buttons to press in which order)
      - What you expect to happen (in detail, e.g., before each PRE element,
      there should appear the text "Copy All").
      - What really happens (in detail)

      We should be able to try to reproduce the problem, recognize the problem
      when it happens, and know what the desired behavior should be, in order
      to find and correct the bug.
      [color=blue]
      > Can anyone help me?[/color]

      Maybe :)
      [color=blue]
      > function makeIt() {
      > var where = document.getEle mentsByTagName( "TABLE")[4].childNodes[0].childNodes[0].childNodes[0].childNodes;[/color]

      The fourth table's first child's first child's first child's children.

      That would be the children of the first cell of the first row of the
      first rowgroup (which is probably a tbody), correct?

      Why not just give that td an id? Or use
      document.getEle mentsByTagName( "table").ro ws[0].cells[0].childNodes;
      ?
      [color=blue]
      > var base = document.getEle mentsByTagName( "PRE")[0];[/color]

      First PRE in the document. Not used?
      [color=blue]
      > var link = document.create Element("SPAN") ;
      > var text = document.create TextNode("Copy All");
      > link.appendChil d(text);[/color]

      So "link" is "<span>Copy All</span>"
      [color=blue]
      > for (var i=0;i<where.len gth;i++) {
      > if (where[i].nodeName == 'PRE'))
      > document.body.i nsertBefore(lin k,where[i]);[/color]

      If you find more than one PRE, insert the link before each - in
      effect, insert it before the last one, since one node can only appear
      once in a document. Inserting it again will move it from its previous
      position.

      How about:

      ---
      function makeIt() {
      var where = document.getEle mentsByTagName( "table")[4].firstChild.fir stChild.firstCh ild;
      var pres = where.getElemen tsByTagName("pr e");
      for (var i=0;i<pres.leng th;i++) {
      var pre = pres[i];
      var link = document.create Element("span") ;
      link.appendChil d(document.crea teTextNode("Cop y All"));
      // or make it a link or something
      pre.parentNode. insertBefore(li nk,pre);
      }
      }
      ---
      (not tested, as I don't have a suitable test page).

      /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

      • Dante

        #4
        Re: Need help with insertBefore

        Sorry I was too vague. I don't know what caused the "Invalid Argument"
        with insertBefore. I hate those errors that don't tell me what's
        wrong; they just say something's wrong.

        Thanks.

        Comment

        Working...