Event.stopPropagation.html (2561B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title> W3C DOM Level 3 Event Object method: stopPropagation </title> 5 <script type="text/javascript"> 6 var PassTest = function() 7 { 8 document.getElementById("testresult").firstChild.data = "PASS"; 9 } 10 11 var FailTest = function() 12 { 13 document.getElementById("testresult").firstChild.data = "FAIL"; 14 } 15 16 var EVENT = "mousedown"; 17 var TARGET, PARENT; 18 var ActualResult = []; 19 var ExpectResult = []; 20 21 window.onload = function() 22 { 23 try 24 { 25 TARGET = document.getElementById("target"); 26 PARENT = document.getElementById("parent"); 27 28 ExpectResult = [document.body, PARENT]; 29 30 TARGET.addEventListener(EVENT, TestEvent, true); 31 TARGET.addEventListener(EVENT, TestEvent, false); 32 PARENT.addEventListener(EVENT, TestEvent, true); 33 PARENT.addEventListener(EVENT, TestEvent, false); 34 document.body.addEventListener(EVENT, TestEvent, true); 35 } 36 catch(ex) 37 { 38 FailTest(); 39 } 40 } 41 42 function TestEvent(evt) 43 { 44 ActualResult.push(evt.currentTarget); 45 46 if (evt.currentTarget == PARENT) 47 { 48 try 49 { 50 evt.stopPropagation(); 51 } 52 catch(ex) 53 { 54 } 55 } 56 } 57 58 function VerifyTest() 59 { 60 if (ActualResult.toString() == ExpectResult.toString()) 61 { 62 PassTest(); 63 } 64 else 65 { 66 FailTest(); 67 } 68 } 69 </script> 70 </head> 71 <body> 72 <h4> 73 Test Description: 74 stopPropagation prevents other event listeners from being triggered. 75 </h4> 76 77 <div id="parent"> 78 Click the button: <input id="target" type="button" value="Target" onmouseup="VerifyTest()" /> 79 </div> 80 81 <p>Test passes if the word "PASS" appears below after clicking the above button using mouse.</p> 82 <div>Test result: </div> 83 <div id='testresult'>FAIL</div> 84 </body> 85 </html>