Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using javascript, how can I tell if an HTML object in the DOM is not in the HTML source code?

I'm looking for ideas on how to detect objects that are in the HTML DOM, but they are not represented explicitly in the HTML source.

As you may know, if I have HTML source that contains the following:

<table id="myTbl">
    <tr id="row1">
        <td id="name">George</td>
    </tr>
</table>

...the HTML DOM will add the <tbody> object in the object tree without changing the source code, understanding that the source code implies it. So in the DOM, the structure is as if the HTML source had been:

<table id="myTbl">
    <tbody>
        <tr id="row1">
            <td id="name">George</td>
        </tr>
    </tbody>
</table>

I have a javascript function that is going through the DOM tree, and I need to detect when I have run across an object that is implied, that is, it is in the DOM, but not in the HTML source.

Any ideas how to do this? Maybe there is some attribute in the object that tells whether it originated from the source or not?

like image 674
Jonathan M Avatar asked May 18 '11 15:05

Jonathan M


Video Answer


2 Answers

May be keep at the document.body.onload the string of the initial document.body.innerHTML

Then when you want to make the check, test first to see if they are still the same.
If not compare both strings and find the differences.

I guess this is ok, if you don't have a too heavy page.

like image 168
Mic Avatar answered Oct 12 '22 03:10

Mic


Reading your comment you just need to determine if a <tbody> tag has been added by the rendering process as opposed to being present in the source?

If so why not modify the source that does contain <tbody> by applying an attribute <tbody class="exp">, then as you walk the DOM you know that any tbody node not possessing that attribute was inserted by the rendering engine.

like image 28
Alex K. Avatar answered Oct 12 '22 03:10

Alex K.