Need help with insertBefore

D

Dante

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.getElementsByTagName("TABLE")[4].childNodes[0].childNodes[0].childNodes[0].childNodes;
var base = document.getElementsByTagName("PRE")[0];
var link = document.createElement("SPAN");
var text = document.createTextNode("Copy All");
link.appendChild(text);
for (var i=0;i<where.length;i++) {
if (where.nodeName == 'PRE'))
document.body.insertBefore(link,where);
}
}

Thank you so much, Dante.
 
M

Martin Honnen

Dante said:
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.getElementsByTagName("TABLE")[4].childNodes[0].childNodes[0].childNodes[0].childNodes;

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.getElementsByTagName
or loop through childNodes but make sure that you check for nodeType and
tagName.
 
L

Lasse Reichstein Nielsen

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.

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.
Can anyone help me?

Maybe :)
function makeIt() {
var where = document.getElementsByTagName("TABLE")[4].childNodes[0].childNodes[0].childNodes[0].childNodes;

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.getElementsByTagName("table").rows[0].cells[0].childNodes;
?
var base = document.getElementsByTagName("PRE")[0];

First PRE in the document. Not used?
var link = document.createElement("SPAN");
var text = document.createTextNode("Copy All");
link.appendChild(text);

So "link" is said:
for (var i=0;i<where.length;i++) {
if (where.nodeName == 'PRE'))
document.body.insertBefore(link,where);


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.getElementsByTagName("table")[4].firstChild.firstChild.firstChild;
var pres = where.getElementsByTagName("pre");
for (var i=0;i<pres.length;i++) {
var pre = pres;
var link = document.createElement("span");
link.appendChild(document.createTextNode("Copy All"));
// or make it a link or something
pre.parentNode.insertBefore(link,pre);
}
}
 
D

Dante

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top