Capturing and Bubbling: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (pandora bracelet)
(Undo spam overwrite by Pandora033)
 
Line 1: Line 1:
At this time light Zhan in the ground of sole <a href="http://pandora.charmclub-uk.com/">pandora uk</a> of foot, spread the voice of small Long Long, several individuals was immediately vigilant.The wood hand of green Luo fat Du Du moved sooner and rounded of turn the son is also more and more big.
In Javascript, events ''bubble'' up and down the DOM heirarchy.
The floor quack Zhi Zhi ground spilted open a way way thin scar, Wang Tian is afloat hurried at the half is <a href="http://pandora.charmclub-uk.com/">pandora charm</a> empty in.Ground the Shan order thin ground ray of light now at the same time, several steps of the countermarch of Wu Qi's instinct.
In other words, every event (like a click, mouseover, etc.) is "visible" both on the directly related element, and all of its parents.
"Do not worry, this was the plant of green Luo to start growing, it just hair letter interest, the enemy came over from the underground!"The small treasure quickly reminds that Wu <a href="http://pandora.charmclub-uk.com/">pandora charms uk</a> rises and Wang Tian.
 
The enemy <a href="http://pandora.charmclub-uk.com/">pandora bracelet</a> strikes from the dark place, not know is what things ……run also not BE, wait helplessly for the end isn't also way!Drilling from the thin ground ray of light is green of rattan, tremble extension upward, gradually bulky, still take on the barren Jing sharp-edged of point sting, suddenly and together brush ground to turn around dynasty the ground of the light Zhan firm go, start to splash a slice slice crushed stones, submerged underground, obviously discovered the enemy of underground delitescence.
The practical upshot of this is that you can act on events all throughout the document with a single listener.
<a href="http://www.clevertester.com/forums/topic.php?id=452386&replies=1#post-491004">http://www.clevertester.com/forums/topic.php?id=452386&replies=1#post-491004</a>
 
== Catch Bubbling Events ==
 
By attaching an event listener to the <code>document</code> (or any other element), one can monitor all events that happen on all child and descendant nodes.
Or, by doing:
 
<pre class='sample-good'>document.addEventListener('click', clickHandler, true);</pre>
 
Then the <code>clickHandler</code> function will be called for every click on every element in the document, so:
 
<pre class='sample-good'>
function clickHandler(event) {
  GM_log("User clicked on element: " + event.target);
}
</pre>
 
Will print a message to the log for every click in the document.
 
'''Note:''' The third parameter to <code>addEventListener</code> can be a bit mysterious.
It is a result of the conflicting event propagation models initially used in early versions of Netscape and Internet Explorer.
Authors should read the "Event Order" article linked in the see also section for a detailed explanation of its meaning and implications.
 
== Monitoring Document Changes ==
 
This technique works for all Javascript event types, notably including the "DOM Mutation" events.
As the name implies, these events are concerned with the DOM mutating, or changing.
They are thus a powerful technique for monitoring pages that change (i.e. as a result of an AJAX call), and re-applying the script's enhancement to the new content in the page.
So, for example:
 
<pre class='sample-good'>
document.addEventListener("DOMNodeInserted", nodeInsertedHandler, false);
 
function nodeInsertedHandler(event) {
  GM_log("element with id " + event.target.id + " has been added");
}
</pre>
 
This script will be able to keep track of all the new nodes being inserted into the document.
See the [http://www.squarefree.com/2005/05/22/autolink/ AutoLink] script for a detailed example of this technique applied in a real script.
Of special importance is the fact that this script is careful to not process its own modifications to the DOM, which would otherwise cause an infinite recursion.
(Look for the <code>nodeInserted</code> function.)
 
== See Also ==
 
* [http://www.quirksmode.org/js/events_order.html Javascript - Event Order]
 
[[Category:Coding Tips]]

Latest revision as of 11:56, 3 December 2012

In Javascript, events bubble up and down the DOM heirarchy. In other words, every event (like a click, mouseover, etc.) is "visible" both on the directly related element, and all of its parents.

The practical upshot of this is that you can act on events all throughout the document with a single listener.

Catch Bubbling Events

By attaching an event listener to the document (or any other element), one can monitor all events that happen on all child and descendant nodes. Or, by doing:

document.addEventListener('click', clickHandler, true);

Then the clickHandler function will be called for every click on every element in the document, so:

function clickHandler(event) {
  GM_log("User clicked on element: " + event.target);
}

Will print a message to the log for every click in the document.

Note: The third parameter to addEventListener can be a bit mysterious. It is a result of the conflicting event propagation models initially used in early versions of Netscape and Internet Explorer. Authors should read the "Event Order" article linked in the see also section for a detailed explanation of its meaning and implications.

Monitoring Document Changes

This technique works for all Javascript event types, notably including the "DOM Mutation" events. As the name implies, these events are concerned with the DOM mutating, or changing. They are thus a powerful technique for monitoring pages that change (i.e. as a result of an AJAX call), and re-applying the script's enhancement to the new content in the page. So, for example:

document.addEventListener("DOMNodeInserted", nodeInsertedHandler, false);

function nodeInsertedHandler(event) {
  GM_log("element with id " + event.target.id + " has been added");
}

This script will be able to keep track of all the new nodes being inserted into the document. See the AutoLink script for a detailed example of this technique applied in a real script. Of special importance is the fact that this script is careful to not process its own modifications to the DOM, which would otherwise cause an infinite recursion. (Look for the nodeInserted function.)

See Also