Cancelling Events with JavaScript
This page features some very basic tests of DOM event registration and cancelling the popagation of events using the traditional event model, W3C DOM Level 2 and Microsoft stuff. The results aren’t very suprising, but will hopefully serve as a good reference.
These tests were conducted in the most current versions of Internet Explorer, Firefox, Safari1, OmniWeb and Opera. Descriptions of the tests, as well as live test are available here.
Stopping Events set with DOM Traditonal Event Registraton
- function(e) {}
- function(e) {e.preventDefault();}
- function() {window.event.returnValue=false;}
- function(e) {e.returnValue=false;}
- function() {return false;}
| Function | Internet Explorer 6 | Firefox 1.5.4 | Safari 2.0.2 | OmniWeb 5.5b4 | Opera 9.01 |
|---|---|---|---|---|---|
| function(e) {e.preventDefault();} | No | Yes | Yes | Yes | Yes |
| function() {window.event.returnValue=false;} | Yes | No | Yes | Yes | Yes |
| function(e) {e.returnValue=false;} | No | No | Yes | Yes | Yes |
| function() {return false;} | Yes | Yes | Yes | Yes | Yes |
Stopping Events set with W3C DOM Level 2 Event Registraton
- function(e) {}
- function(e) {e.preventDefault();}
- function() {window.event.returnValue=false;}
- function(e) {e.returnValue=false;}
- function() {return false;}
| Function | Internet Explorer 6 | Firefox 1.5.4 | Safari 2.0.2 | OmniWeb 5.5b4 | Opera 9.01 |
|---|---|---|---|---|---|
| function(e) {e.preventDefault();} | No Event | Yes | Yes | Yes | Yes |
| function() {window.event.returnValue=false;} | No Event | No | Yes | Yes | Yes |
| function(e) {e.returnValue=false;} | No Event | No | Yes | Yes | Yes |
| function() {return false;} | No Event | No | No | No | No |
Stopping Events set with Microsoft Proprietary Event Registraton
- function(e) {}
- function(e) {e.preventDefault();}
- function() {window.event.returnValue=false;}
- function(e) {e.returnValue=false;}
- function() {return false;}
| Function | Internet Explorer 6 | Firefox 1.5.4 | Safari 2.0.2 | OmniWeb 5.5b4 | Opera 9.01 |
|---|---|---|---|---|---|
| function(e) {e.preventDefault();} | No | No Event | No Event | No Event | Yes |
| function() {window.event.returnValue=false;} | Yes | No Event | No Event | No Event | Yes |
| function(e) {e.returnValue=false;} | Yes | No Event | No Event | No Event | Yes |
| function() {return false;} | Yes | No Event | No Event | No Event | No |
Note: Safari fails the control test for these
events, by not changing the fragment section of the URL after the
event fires. In order to test with Safari, the links were altered to a slightly
more cumbersome href="/test-#-failed". ↩