A bug detailed explanation about JavaScript event Flow _javascript tips

Source: Internet
Author: User

I recently tuned NetSurf also encountered a related Bug:alert () was transferred two times. HTML code:

Copy Code code as follows:

<title>alert onclick example</title>

<script type= "Text/javascript" >

function Causealert ()
{
var txt = document.getElementById ("P1"). Textcontent;
alert (TXT);
}

</script>

<body>
<div style= "border:1px solid Red" >
<p id= "P1" >first line of Paragraph.<br/></p>
</div><br/>

<button id= "Button1" >add another textnode.</button>

<script>
var Button1 = document.getElementById ("button1");
/*var Button1Click = function () {alert (1);};
Button1.addeventlistener ("Click", Button1Click, false);
Button1.onclick = Causealert;

</script>

</body>


Via GDB, navigate to the problem within _dom_node_dispatch_event ():
Copy Code code 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 this: P1 (root)-->p2--> ...--> pm--> t (capturing phase), T (target phase), t--> pm--> ...--> p 1 (bubbling phase).

The specification stipulates capturing and bubbling can only choose one, in the code in Js_dom_event_add_listener () is chooses the capturing. So it explains why alert was executed two back.

Modify: According to the DOM 3 specification, the above 0 is changed to 1 on the line.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.