473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

appendChild

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

var start, newElement;
start = document.body;
newElement = document.createElement('b');
newText = document.createTextNode("The Greasemonkey salutes you!");
newElement.appendChild(newText);
start.appendChild(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.

Jul 23 '05 #1
3 6846
On 18/06/2005 19:09, jacster wrote:

[snip]
var start, newElement;
start = document.body;
newElement = document.createElement('b');
newText = document.createTextNode("The Greasemonkey salutes you!");
newElement.appendChild(newText);
start.appendChild(newElement);

Can anyone tell me: 1) why my string appears twice
It shouldn't, and it doesn't (not here, anyway).

[snip]
I'm trying to insert code at the top of every viewed page.
Server-side includes would seem to be a better solution.
Obviously, document.body appends the string to the closing body tag
The appendChild method appends the B element as the last child node of
the BODY element.

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


The insertBefore method.

var body = document.body,
element, text;

if(body && body.insertBefore && document.createTextNode
&& document.createElement)
{
element = document.createElement('b');
text = document.createTextNode('The Greasemonkey salutes you!');

if(element && text && element.appendChild) {
element.appendChild(text);
body.insertBefore(element, body.firstChild);
}
}

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
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.

Jul 23 '05 #3
var lstmsg;
lstmsg = lstmsg.replace(/appendBefore/, "insertBefore");

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
3
by: Robi | last post by:
I have the following code: ############## var nHead=(document.getElementsByTagName)?document.getElementsByTagName("head").item(0):document.head; var nStyle=document.createElement("style"); //...
2
by: samuel.adam | last post by:
Hi all, I am coding an AJAX DHTML whatever application and I was fed up with always typing a lot of appendChild() functions. I created a custom one called append_children() and wanted to share...
2
by: Stewart | last post by:
Originally posted in comp.lang.javascript: Newsgroups: comp.lang.javascript From: "Stewart" Date: 23 Aug 2005 02:50:04 -0700 Local: Tues, Aug 23 2005 10:50 am Subject: FireFox, RemoveChild,...
1
by: xaces | last post by:
I am relatively new to javascript. I am trying to create a menu. I cant get it to show on IE. The object exists but just wouldnt show up. Can anyone please help?? Thanks :confused: var...
7
by: Robert Oschler | last post by:
I'm having a very painful time converting some Mozilla dynamic DOM code to work with Internet Explore. For example, given this code: -------------- selectBox=document.createElement("SELECT");...
4
by: matty | last post by:
Hello, here is an example of what is driving me totally crazy. This example will show that the first call to "doit()" will print 30 times the image with only one call to the server. Then I have...
3
by: ezmiller | last post by:
So I have some code that gets the body element of another frame and then tries to dynamically write a table. The code fails when, after creating the table, I try to append it to the document. I...
2
by: vsanjit | last post by:
Hi all, I've been trying to create a table dynamically upon the generation of en event using the appendChild method in Javascript. This seems to work fine in Firefox, but not in IE7. There's also...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.