92 lines
3 KiB
HTML
92 lines
3 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
|
|
|
|
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
<title>Simple Event Handling and Processing</title>
|
|
|
|
<style type="text/css">
|
|
/*margin and padding on body element
|
|
can introduce errors in determining
|
|
element position and are not recommended;
|
|
we turn them off as a foundation for YUI
|
|
CSS treatments. */
|
|
body {
|
|
margin:0;
|
|
padding:0;
|
|
}
|
|
</style>
|
|
|
|
<link rel="stylesheet" type="text/css" href="../../build/fonts/fonts-min.css" />
|
|
<script type="text/javascript" src="../../build/yahoo-dom-event/yahoo-dom-event.js"></script>
|
|
|
|
<!--there is no custom header content for this example-->
|
|
|
|
</head>
|
|
|
|
<body class="yui-skin-sam">
|
|
|
|
|
|
<h1>Simple Event Handling and Processing</h1>
|
|
|
|
<div class="exampleIntro">
|
|
<p>Clicking in the blue box will pop up a "Hello World!" alert window. Clicking on the first link will take you to the YUI website; clicking on the second link, which has the same <code>href</code> attribute, will pop up an alert instead and not navigate to a new page.</p>
|
|
|
|
<p>Event Utility is used here to do two things:</p>
|
|
|
|
<ol>
|
|
<li>Attach the <code>click</code> event handler to the blue box;</li>
|
|
<li>Attach an event handler to the second <code><a></code> element that uses <code>YAHOO.util.Event.preventDefault()</code> to prevent the link, when clicked, from navigating to a new page. </li>
|
|
</ol>
|
|
</div>
|
|
|
|
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== -->
|
|
|
|
<script>
|
|
|
|
(function() {
|
|
|
|
//A function that pops up a "Hello World" alert:
|
|
var helloWorld = function(e) {
|
|
YAHOO.log("helloWorld function firing.", "info", "example");
|
|
alert("Hello World!");
|
|
}
|
|
|
|
//subscribe the helloWorld function as an event
|
|
//handler for the click event on the container
|
|
//div:
|
|
YAHOO.util.Event.addListener("container", "click", helloWorld);
|
|
|
|
//A function that pops up an alert and
|
|
//prevents the default behavior of the event
|
|
//for which it is a handler:
|
|
var interceptLink = function(e) {
|
|
YAHOO.util.Event.preventDefault(e);
|
|
YAHOO.log("You clicked on the second YUI link.", "info", "example");
|
|
alert("You clicked on the second YUI link.");
|
|
}
|
|
|
|
//subscribe our interceptLink function
|
|
//as a click handler for the second anchor
|
|
//element:
|
|
YAHOO.util.Event.addListener("secondA", "click", interceptLink);
|
|
|
|
//log message to indicate that the example is ready:
|
|
YAHOO.log("When you begin interacting with the example at left, you'll see log messages appear here.", "info", "example");
|
|
|
|
})();
|
|
</script>
|
|
|
|
<style>
|
|
#container {background-color:#00CCFF; border:1px dotted black; padding:1em; cursor:pointer;}
|
|
</style>
|
|
<div id="container">
|
|
<p>Click for Hello World alert.</p>
|
|
</div>
|
|
<p><a href="http://developer.yahoo.com/yui" id="firstA">The YUI Library. (Link navigates away from page.)</a></p>
|
|
<p><a href="http://developer.yahoo.com/yui" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a></p>
|
|
<!--END SOURCE CODE FOR EXAMPLE =============================== -->
|
|
|
|
</body>
|
|
</html>
|