Created
June 9, 2014 01:48
-
-
Save Williammer/070b2cc7bedbfc9c1ca8 to your computer and use it in GitHub Desktop.
javaScriptPlayEvent.js - Event practices on a variety of things compatible with different browsers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //change "Click me: + count" : count add 1 | |
| function myHandler(e) { | |
| var src, parts; | |
| // get event and source element | |
| e = e || window.event; | |
| src = e.target || e.srcElement; | |
| // actual work: update label | |
| parts = src.innerHTML.split(": "); | |
| parts[1] = parseInt(parts[1], 10) + 1; | |
| src.innerHTML = parts[0] + ": " + parts[1]; | |
| // no bubble | |
| if (typeof e.stopPropagation === "function") { | |
| e.stopPropagation(); | |
| } | |
| if (typeof e.cancelBubble !== "undefined") { | |
| e.cancelBubble = true; | |
| } | |
| // prevent default action | |
| if (typeof e.preventDefault === "function") { | |
| e.preventDefault(); | |
| } | |
| if (typeof e.returnValue !== "undefined") { | |
| e.returnValue = false; | |
| } | |
| } | |
| var b = document.getElementById('clickme'); | |
| if (document.addEventListener) { // W3C | |
| b.addEventListener('click', myHandler, false); | |
| } else if (document.attachEvent) { // IE | |
| b.attachEvent('onclick', myHandler); | |
| } else { // last resort | |
| b.onclick = myHandler; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment