Obtaining the last inserted node.

D

Daz

Hi.

I would like to know how to obtain a reference to (or at least, element
type of) the last node which was inserted into the document. I am using
an event listener to listen for dom inserts, but I am not sure how to
check if that last node was a script tag.

Please could someone point me in the right direction?

Many thanks, and Merry Christmas.

Daz.
 
M

Martin Honnen

Daz said:
I would like to know how to obtain a reference to (or at least, element
type of) the last node which was inserted into the document. I am using
an event listener to listen for dom inserts, but I am not sure how to
check if that last node was a script tag.

If that is the W3C DOM Level 2 DOMNodeInserted event e.g.

document.addEventListener('DOMNodeInserted', function (evt) {
alert(evt.target + ' inserted under parent ' + evt.relatedNode);
}, false);

then the evt.target tells you the inserted node and evt.relatedNode the
parent node into which evt.target has been inserted. So you can check
whether evt.target is an element node and if so, its
tagName.toLowerCase() is 'script'.
 
D

Daz

Martin said:
If that is the W3C DOM Level 2 DOMNodeInserted event e.g.

document.addEventListener('DOMNodeInserted', function (evt) {
alert(evt.target + ' inserted under parent ' + evt.relatedNode);
}, false);

then the evt.target tells you the inserted node and evt.relatedNode the
parent node into which evt.target has been inserted. So you can check
whether evt.target is an element node and if so, its
tagName.toLowerCase() is 'script'.
Fantastic!

Thanks Martin. :)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top