A bug in javascript event flow

Source: Internet
Author: User

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

Copy codeThe Code is as follows:
<Html>
<Head>
<Title> alert onclick example </title>

<Script type = "text/javascript">

Function causealert ()
{
Var txt = document. getElementById ("p1"). textContent;
Alert (txt );
}

</Script>
</Head>

<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>
</Html>

Through gdb, locate the problem in _ dom_node_dispatch_event:
Copy codeThe 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 stream is like this: p1 (root) --> p2 -->... --> pm --> T (capturing phase), T (target phase), T --> pm -->... --> p1 (bubbling phase ).

The specification specifies that capturing and bubbling can only be selected. In the code, the js_dom_event_add_listener () option is capturing. This explains why alert was executed twice.

Modify: Change the preceding 0 to 1 according to the DOM 3 specification.

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.