appendChild

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

    appendChild

    Hi,
    I'm using the following code in a Greasemonkey script.

    var start, newElement;
    start = document.body;
    newElement = document.create Element('b');
    newText = document.create TextNode("The Greasemonkey salutes you!");
    newElement.appe ndChild(newText );
    start.appendChi ld(newElement);

    Can anyone tell me: 1) why my string appears twice; 2) how to make it
    appear just once?

    I'm trying to insert code at the top of every viewed page. Obviously,
    document.body appends the string to the closing body tag which is not
    what I want. What could I use to insert my code as the first childnode
    under 'body'?

    Thanks.

  • Michael Winter

    #2
    Re: appendChild

    On 18/06/2005 19:09, jacster wrote:

    [snip]
    [color=blue]
    > var start, newElement;
    > start = document.body;
    > newElement = document.create Element('b');
    > newText = document.create TextNode("The Greasemonkey salutes you!");
    > newElement.appe ndChild(newText );
    > start.appendChi ld(newElement);
    >
    > Can anyone tell me: 1) why my string appears twice[/color]

    It shouldn't, and it doesn't (not here, anyway).

    [snip]
    [color=blue]
    > I'm trying to insert code at the top of every viewed page.[/color]

    Server-side includes would seem to be a better solution.
    [color=blue]
    > Obviously, document.body appends the string to the closing body tag[/color]

    The appendChild method appends the B element as the last child node of
    the BODY element.

    <!-- content --> <- new element inserted after this point.
    </body>
    [color=blue]
    > What could I use to insert my code as the first childnode under
    > 'body'?[/color]

    The insertBefore method.

    var body = document.body,
    element, text;

    if(body && body.insertBefo re && document.create TextNode
    && document.create Element)
    {
    element = document.create Element('b');
    text = document.create TextNode('The Greasemonkey salutes you!');

    if(element && text && element.appendC hild) {
    element.appendC hild(text);
    body.insertBefo re(element, body.firstChild );
    }
    }

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • jacster

      #3
      Re: appendChild

      Hi Mike,
      Thanks for the appendBefore and body.firstChild tips. Changed my script
      and now the double-string problem is gone and text at page top.
      Server side includes aren't that suitable because my code is for giving
      the browsing user expanded parse/report/log options on remote pages.
      Did consdier filtering/changing remote pages through a local server
      with PHP for extended language functions but want to get js working
      first.
      Cheers.

      Comment

      • jacster

        #4
        Re: appendChild

        var lstmsg;
        lstmsg = lstmsg.replace(/appendBefore/, "insertBefore") ;

        Comment

        Working...