Home  >  Article  >  Web Front-end  >  Detailed explanation of a bug in javascript event flow_javascript skills

Detailed explanation of a bug in javascript event flow_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:22:22938browse

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.

Statement:
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
Previous article:Javascript sample code to obtain the actual size of the web page window_javascript skillsNext article:Javascript sample code to obtain the actual size of the web page window_javascript skills

Related articles

See more