Created
July 2, 2012 21:42
-
-
Save sethkrasnianski/3035895 to your computer and use it in GitHub Desktop.
JavaScript Events
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
<!DOCTYPE html> | |
<!-- | |
TITLE: JavaScript Events | |
AUTHOR: Seth Krasnianski | |
PURPOSE: Basic 'click' event and event handler in JavaScript. | |
--> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>JavaScript - Events</title> | |
</head> | |
<body> | |
<a href="#" id="anchor">Click Here!</a> | |
<script type="text/javascript"> | |
var anchor = document.getElementById('anchor'); | |
function doSomething() { | |
alert('clicked'); | |
} | |
function addEvent(obj, evt, fn, capture) { | |
//Detect if IE | |
if(obj.attachEvent) { | |
obj.attachEvent('on' + evt, fn); | |
} | |
else { | |
//Detect if browser uses capture phase | |
if(!capture) capture = false; | |
obj.addEventListener(evt, fn, capture); | |
} | |
} | |
addEvent(anchor, 'click', doSomething); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment