Home > Web Front-end > JS Tutorial > body text

Detailed explanation of a bug in javascript event flow_javascript skills

WBOY
Release: 2016-05-16 17:22:22
Original
962 people have browsed it

I recently encountered a related bug when tuning netsurf: alert() was called twice. html code:

Copy code The code is as follows:



alert onclick example




First line of paragraph.
< /p>


<script><br>var Button1 = document.getElementById("button1");<br>/*var Button1Click = function() { alert(1); }; <br>Button1.addEventListener("click ", Button1Click, false);*/<br>Button1.onclick = causealert;</p> <p></script>




Through gdb, locate the problem in _dom_node_dispatch_event():
Copy code The code is as follows:

/* The capture phase */ for (targetnr = ntargets; targetnr > 0; --targetnr). ..

/* Bubbling phase */
evt->phase = DOM_BUBBLING_PHASE;

for (targetnr = 0; targetnr < ntargets; targetnr)


The event flow is like this: p1(root)-->p2-->... --> pm --> T (capturing phase), T (target phase), T--> ; pm --> ... --> p1 (bubbling phase).

The specification stipulates that only one of capturing and bubbling can be selected. In the code, capturing is selected in js_dom_event_add_listener(). This explains why alert is executed twice.

Modification: According to the DOM 3 specification, just change the above 0 to 1.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template