Javascript listen for innerhtml change
|
I need to intercept any changes in the content of a cell inside my webpage.
Show
The following code shows me that addEventListener does not work.
The code is just a toy example. In my real case the changes in the page (and therefore in the cell, too) are made by a webapp that I have NO control over. asked Sep 11, 2011 at 21:14 2 There is a modern way to catch innerhtml changes: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe Example:
childList mutation fires on innerHTML change. answered May 18, 2020 at 9:20 legalelegale 4524 silver badges8 bronze badges 1 You can't listen to a DOM element There is some other new DOM 3 events that would help you on this. Here is some: DOMCharacterDataModified //Draft DOMSubtreeModified answered Sep 11, 2011 at 21:22 MohsenMohsen 62.7k33 gold badges155 silver badges181 bronze badges 1 Does this Most efficient method of detecting/monitoring DOM changes? help? It seems like there aren't any 100% cross browser solutions, and one of the workarounds is to poll the elements of interest to see if their innerHTML.length changes! answered Sep 11, 2011 at 21:22 Paul GrimePaul Grime 14.9k4 gold badges34 silver badges57 bronze badges The To insert the HTML into the document rather than replace the contents of an element, use the method ValueA string containing the HTML serialization of the element's descendants. Setting the value of Exceptions
Thrown if an attempt was made to set the value of NoModificationAllowedError DOMException
Thrown if an attempt was made to insert the HTML into a node whose parent is a Usage notesThe Reading the HTML contents of an elementReading
This lets you look at the HTML markup of the element's content nodes. Note: The returned HTML or XML fragment is generated based on the current contents of the element, so the markup and formatting of the returned fragment is likely not to match the original page markup. Replacing the contents of an elementSetting the value of Note: This is a security risk if the string to be inserted might contain potentially malicious content. When inserting user-supplied data you should always consider using For example, you can erase the entire contents of a document by clearing the contents of the document's
This example fetches the document's current HTML markup and replaces the
Operational details
`; Appending HTML to an elementSetting the value of For example, we can append a new list item (
`; Please note that using Security considerationsIt is not uncommon to see
Although this may look like a cross-site scripting attack, the result is harmless. HTML specifies that a However, there are ways to execute JavaScript without using
For that reason, it is recommended that instead of
ExamplesThis example uses JavaScript
The We add a second method that logs information about
Then we use this as the event handler for a number of mouse events on the box that contains our log:
HTMLThe HTML is quite simple for our example.
The with the class "box" is just a container for layout purposes, presenting the contents with a box around it. The
whose class is "log" is the container for the log text itself.
CSSThe following CSS styles our example content.
ResultThe resulting content looks like this. You can see output into the log by moving the mouse in and out of the box, clicking in it, and so forth. Specifications
Browser compatibilityBCD tables only load in the browser See also |
