|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<title>YUI Library Examples: Event: Simple DOM Events</title> |
|
|
6 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
7 |
<link rel="stylesheet" type="text/css" href="../../assets/yui.css" > |
|
|
8 |
|
|
|
9 |
<style> |
|
|
10 |
/*Supplemental CSS for the YUI distribution*/ |
|
|
11 |
#custom-doc { width: 95%; min-width: 950px; } |
|
|
12 |
#pagetitle {background-image: url(../../assets/bg_hd.gif);} |
|
|
13 |
/* #pagetitle h1 {background-image: url(../../assets/title_h_bg.gif);}*/ |
|
|
14 |
</style> |
|
|
15 |
|
|
|
16 |
<link rel="stylesheet" type="text/css" href="../../assets/dpSyntaxHighlighter.css"> |
|
|
17 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
18 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
19 |
<style type="text/css"> |
|
|
20 |
#container {background-color:#00CCFF; border:1px dotted black; padding:1em; cursor:pointer;} |
|
|
21 |
</style> |
|
|
22 |
</head> |
|
|
23 |
<body id="yahoo-com" class=" yui-skin-sam"> |
|
|
24 |
<div id="custom-doc" class="yui-t2"> |
|
|
25 |
<div id="hd"> |
|
|
26 |
<div id="ygunav"> |
|
|
27 |
<p> |
|
|
28 |
<em> |
|
|
29 |
<a href="http://developer.yahoo.com/yui/3/">YUI 3.x Home</a> <i> - </i> |
|
|
30 |
</em> |
|
|
31 |
</p> |
|
|
32 |
<form action="http://search.yahoo.com/search" id="sitesearchform"> |
|
|
33 |
<input name="vs" type="hidden" value="developer.yahoo.com"> |
|
|
34 |
<input name="vs" type="hidden" value="yuiblog.com"> |
|
|
35 |
<div id="sitesearch"> |
|
|
36 |
<label for="searchinput">Site Search (YDN & YUIBlog): </label> |
|
|
37 |
<input type="text" id="searchinput" name="p"> |
|
|
38 |
<input type="submit" value="Search" id="searchsubmit" class="ygbt"> |
|
|
39 |
</div> |
|
|
40 |
</form> |
|
|
41 |
</div> |
|
|
42 |
<div id="ygma"><a href="../../"><img src="../../assets/logo.gif" border="0" width="200" height="93"></a></div> |
|
|
43 |
<div id="pagetitle"><h1>YUI Library Examples: Event: Simple DOM Events</h1></div> |
|
|
44 |
</div> |
|
|
45 |
<div id="bd"> |
|
|
46 |
|
|
|
47 |
<div id="bar-note"><p><strong>Note:</strong> This is YUI 3.x. Looking for <a href="http://developer.yahoo.com/yui/">YUI 2.x</a>?</p></div> |
|
|
48 |
|
|
|
49 |
<div id="yui-main"> |
|
|
50 |
<div class="yui-b"> |
|
|
51 |
<div class="yui-ge"> |
|
|
52 |
<div class="yui-u first example" id="main"> |
|
|
53 |
|
|
|
54 |
<h2>Event: Simple DOM Events</h2> |
|
|
55 |
|
|
|
56 |
<div id="example" class="promo"> |
|
|
57 |
<p> |
|
|
58 |
<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> |
|
|
59 |
|
|
|
60 |
<p>Event Utility is used here to do two things:</p> |
|
|
61 |
|
|
|
62 |
<ol> |
|
|
63 |
<li>Attach the <code>click</code> event handler to the blue box;</li> |
|
|
64 |
<li>Attach an event handler to the second <code><a></code> element that uses <code>preventDefault()</code> to prevent the link, when clicked, from navigating to a new page. </li> |
|
|
65 |
|
|
|
66 |
</ol> </p> |
|
|
67 |
|
|
|
68 |
<div class="module example-container "> |
|
|
69 |
<div class="hd exampleHd"> |
|
|
70 |
<p class="newWindowButton yui-skin-sam"> |
|
|
71 |
<a href="event-simple_clean.html" target="_blank">View example in new window.</a> |
|
|
72 |
</p> |
|
|
73 |
</div> <div id="example-canvas" class="bd"> |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
<script language="javascript"> |
|
|
81 |
|
|
|
82 |
YUI({base:"../../build/", timeout: 10000}).use("node", |
|
|
83 |
function(Y) { |
|
|
84 |
|
|
|
85 |
//A function that pops up a "Hello World" alert: |
|
|
86 |
var helloWorld = function(e) { |
|
|
87 |
Y.log("helloWorld function firing.", "info", "example"); |
|
|
88 |
alert("Hello World!"); |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
//subscribe the helloWorld function as an event |
|
|
92 |
//handler for the click event on the container |
|
|
93 |
//div: |
|
|
94 |
Y.on("click", helloWorld, "#container"); |
|
|
95 |
|
|
|
96 |
//A function that pops up an alert and |
|
|
97 |
//prevents the default behavior of the event |
|
|
98 |
//for which it is a handler: |
|
|
99 |
var interceptLink = function(e) { |
|
|
100 |
e.preventDefault(); |
|
|
101 |
Y.log("You clicked on the second YUI link.", "info", "example"); |
|
|
102 |
alert("You clicked on the second YUI link. Because *preventDefault* was called, this link will not navigate away from the page."); |
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
//subscribe our interceptLink function |
|
|
106 |
//as a click handler for the second anchor |
|
|
107 |
//element: |
|
|
108 |
Y.on("click", interceptLink, "#secondA"); |
|
|
109 |
|
|
|
110 |
//log message to indicate that the example is ready: |
|
|
111 |
Y.log("When you begin interacting with the example at left, you'll see log messages appear here.", "info", "example"); |
|
|
112 |
}); |
|
|
113 |
|
|
|
114 |
</script> |
|
|
115 |
|
|
|
116 |
<div id="container"> |
|
|
117 |
<p>Click for Hello World alert.</p> |
|
|
118 |
</div> |
|
|
119 |
<p><a href="http://developer.yahoo.com/yui" id="firstA">The YUI Library. (Link navigates away from page.)</a></p> |
|
|
120 |
|
|
|
121 |
<p><a href="http://developer.yahoo.com/yui" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a></p> |
|
|
122 |
|
|
|
123 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
124 |
|
|
|
125 |
|
|
|
126 |
</div> |
|
|
127 |
</div> |
|
|
128 |
</div> |
|
|
129 |
|
|
|
130 |
<h2 class="first">Making Use of the Event Utility's Basic Features</h2> |
|
|
131 |
|
|
|
132 |
<p>The YUI Event Utility is a simple, powerful resource for creating event-driven applications in the browser. In this introductory example, we'll look at how to use Event Utility to listen for a specific user event on a specific element in the DOM. We'll also look at how Event Utility can be used within an event handler to provide additional control.</p> |
|
|
133 |
|
|
|
134 |
<p>To illustrate event handling syntax, we'll create a <code><div></code> and pop up an alert message when that <code><div></code> is clicked on. Begin with the style and markup necessary to make your element visible:</p> |
|
|
135 |
|
|
|
136 |
<textarea name="code" class="HTML" cols="60" rows="1"><style type="text/css"> |
|
|
137 |
#container {background-color:#00CCFF; border:1px dotted black; padding:1em; cursor:pointer;} |
|
|
138 |
</style> |
|
|
139 |
|
|
|
140 |
<div id="container"> |
|
|
141 |
<p>Click for Hello World alert.</p> |
|
|
142 |
</div></textarea> |
|
|
143 |
|
|
|
144 |
<p>Next, create a function that receives a single argument — the event object — and pops up an alert which says "Hello World!":</p> |
|
|
145 |
|
|
|
146 |
<textarea name="code" class="JScript" cols="60" rows="1">//A function that pops up a "Hello World" alert: |
|
|
147 |
var helloWorld = function(e) { |
|
|
148 |
alert("Hello World!"); |
|
|
149 |
}</textarea> |
|
|
150 |
|
|
|
151 |
<p>With our markup on the page and a function that we want to execute when our element is clicked on, we now use Event Utility's <code>on</code> method to attach our <code>helloWorld</code> function as a handler for the click event on the element whose HTML ID is <code>container</code>. <code>on</code> is available for convenience at the top-level of a YUI instance; as a result, it can be referenced as <code>Y.on</code> where Y is a YUI instance:</p> |
|
|
152 |
|
|
|
153 |
<textarea name="code" class="JScript" cols="60" rows="1">//subscribe the helloWorld function as an event |
|
|
154 |
//handler for the click event on the container |
|
|
155 |
//div: |
|
|
156 |
Y.on("click", helloWorld, "#container"); |
|
|
157 |
</textarea> |
|
|
158 |
|
|
|
159 |
<p>Almost all event handling begins with a premise just this simple: We have an element ("container") to which something might happen (eg, it might be clicked) and, when that <em>does</em> happen, we want to do something (eg, <code>helloWorld</code>).</p> |
|
|
160 |
|
|
|
161 |
<p>In some cases, you may want to use some of Event Utility's powerful browser abstraction methods to help you handle your interaction flow during an event. For example, let's say you have two links on the page:</p> |
|
|
162 |
|
|
|
163 |
<textarea name="code" class="JScript" cols="60" rows="1"> <p><a href="http://developer.yahoo.com/yui" id="firstA">The YUI Library. (Link navigates away from page.)</a></p> |
|
|
164 |
<p><a href="http://developer.yahoo.com/yui" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a></p></textarea> |
|
|
165 |
|
|
|
166 |
<p>Let's say that when the second link is clicked you want to pop up an alert window and then prevent the browser from navigating to the page designated in the <code>href</code> attribute. The native browser event object has a <code>preventDefault</code> mechanism, but that mechanism was not successfully implemented across all A-Grade browsers until recently. But the event object passed to your event handler is a facade — not the actual browser event object. On this facade, <code>preventDefault</code> is implemented consistently across browsers. As a result, we can call <code>preventDefault</code> from the event facade just as we would from a native event object and expect it to work consistently across browsers:</p> |
|
|
167 |
|
|
|
168 |
<textarea name="code" class="JScript" cols="60" rows="1">//A function that pops up an alert and |
|
|
169 |
//prevents the default behavior of the event |
|
|
170 |
//for which it is a handler: |
|
|
171 |
var interceptLink = function(e) { |
|
|
172 |
e.preventDefault(); |
|
|
173 |
Y.log("You clicked on the second YUI link.", "info", "example"); |
|
|
174 |
alert("You clicked on the second YUI link. Because *preventDefault* was called, this link will not navigate away from the page."); |
|
|
175 |
} |
|
|
176 |
|
|
|
177 |
//subscribe our interceptLink function |
|
|
178 |
//as a click handler for the second anchor |
|
|
179 |
//element: |
|
|
180 |
Y.on("click", interceptLink, "#secondA");</textarea> |
|
|
181 |
|
|
|
182 |
<p>The key lesson here is that you should treat the event facade (the first argument passed to your event handler) just as you would a native event object.</p> |
|
|
183 |
</div> |
|
|
184 |
<div class="yui-u sidebar"> |
|
|
185 |
|
|
|
186 |
|
|
|
187 |
<div id="examples" class="mod box4"> |
|
|
188 |
<div class="hd"> |
|
|
189 |
<h4> |
|
|
190 |
Event Examples:</h4> |
|
|
191 |
</div> |
|
|
192 |
<div class="bd"> |
|
|
193 |
<ul> |
|
|
194 |
<li class='selected'><a href='../event/event-simple.html'>Simple DOM Events</a></li><li><a href='../event/event-timing.html'>Using 'available', 'contentready', and 'domready'</a></li><li><a href='../event/event-ce.html'>Using Custom Events</a></li><li><a href='../event/event-focus-blur.html'>Using the 'focus' and 'blur' custom events</a></li><li><a href='../yui/yui-augment.html'>Compose Classes of Objects with <code>augment</code> (included with examples for The YUI Global Object)</a></li><li><a href='../node-focusmanager/node-focusmanager-3.html'>Accessible Menu Button (included with examples for Focus Manager Node Plugin)</a></li><li><a href='../node-focusmanager/node-focusmanager-2.html'>Accessible TabView (included with examples for Focus Manager Node Plugin)</a></li><li><a href='../attribute/attribute-event.html'>Change Events (included with examples for Attribute)</a></li><li><a href='../widget/widget-extend.html'>Extending the base widget class (included with examples for Widget)</a></li><li><a href='../attribute/attribute-getset.html'>Getters, Setters and Validators (included with examples for Attribute)</a></li><li><a href='../dd/photo-browser.html'>Photo Browser (included with examples for Drag & Drop)</a></li> </ul> |
|
|
195 |
</div> |
|
|
196 |
</div> |
|
|
197 |
|
|
|
198 |
<div class="mod box4"> |
|
|
199 |
<div class="hd"> |
|
|
200 |
<h4>More Event Resources:</h4> |
|
|
201 |
</div> |
|
|
202 |
<div class="bd"> |
|
|
203 |
<ul> |
|
|
204 |
<!-- <li><a href="http://developer.yahoo.com/yui/event/">User's Guide</a> (external)</li> --> |
|
|
205 |
<li><a href="../../api/module_event.html">API Documentation</a></li> |
|
|
206 |
</ul> |
|
|
207 |
</div> |
|
|
208 |
</div> |
|
|
209 |
</div> |
|
|
210 |
</div> |
|
|
211 |
|
|
|
212 |
</div> |
|
|
213 |
</div> |
|
|
214 |
|
|
|
215 |
|
|
|
216 |
<div class="yui-b toc3" id="tocWrapper"> |
|
|
217 |
<!-- TABLE OF CONTENTS --> |
|
|
218 |
<div id="toc"> |
|
|
219 |
|
|
|
220 |
<ul> |
|
|
221 |
<li class="sect first">YUI 3.x Project</li><li class="item"><a title="The Yahoo! User Interface (YUI) Library, 3.x Branch, " href="http://developer.yahoo.com/yui/3/">YUI 3 Web Site (external)</a></li><li class="item"><a title="Examples of every YUI utility and control in action" href="../../examples/">YUI 3 Examples</a></li><li class="item"><a title="Instantly searchable API documentation for the entire YUI library." href="../../api/">YUI 3 API Docs</a></li><li class="item"><a title="The YUI Library can be downloaded from SourceForge" href="http://sourceforge.net/projects/yui/">YUI 3 on Sourceforge (external)</a></li><li class="item"><a title="YUI is free and open, offered under a BSD license." href="http://developer.yahoo.com/yui/3/license.html">YUI License (external)</a></li><li class="sect">YUI 3 Core - Examples</li><li class="item"><a title="YUI (Global Prerequisite) - Functional Examples" href="../../examples/yui/index.html">YUI (Global Prerequisite)</a></li><li class="selected "><a title="Event - Functional Examples" href="../../examples/event/index.html">Event</a></li><li class="item"><a title="Node - Functional Examples" href="../../examples/node/index.html">Node</a></li><li class="sect">YUI 3 Component Infrastructure - Examples</li><li class="item"><a title="Attribute - Functional Examples" href="../../examples/attribute/index.html">Attribute</a></li><li class="item"><a title="Plugin - Functional Examples" href="../../examples/plugin/index.html">Plugin</a></li><li class="item"><a title="Widget - Functional Examples" href="../../examples/widget/index.html">Widget</a></li><li class="sect">YUI 3 Utilities - Examples</li><li class="item"><a title="Animation - Functional Examples" href="../../examples/anim/index.html">Animation</a></li><li class="item"><a title="Cache - Functional Examples" href="../../examples/cache/index.html">Cache</a></li><li class="item"><a title="Cookie - Functional Examples" href="../../examples/cookie/index.html">Cookie</a></li><li class="item"><a title="DataSchema - Functional Examples" href="../../examples/dataschema/index.html">DataSchema</a></li><li class="item"><a title="DataSource - Functional Examples" href="../../examples/datasource/index.html">DataSource</a></li><li class="item"><a title="DataType - Functional Examples" href="../../examples/datatype/index.html">DataType</a></li><li class="item"><a title="Drag & Drop - Functional Examples" href="../../examples/dd/index.html">Drag & Drop</a></li><li class="item"><a title="Get - Functional Examples" href="../../examples/get/index.html">Get</a></li><li class="item"><a title="History - Functional Examples" href="../../examples/history/index.html">History</a></li><li class="item"><a title="ImageLoader - Functional Examples" href="../../examples/imageloader/index.html">ImageLoader</a></li><li class="item"><a title="IO - Functional Examples" href="../../examples/io/index.html">IO</a></li><li class="item"><a title="JSON (JavaScript Object Notation) - Functional Examples" href="../../examples/json/index.html">JSON</a></li><li class="item"><a title="Queue - Functional Examples" href="../../examples/queue/index.html">Queue</a></li><li class="item"><a title="Stylesheet - Functional Examples" href="../../examples/stylesheet/index.html">Stylesheet</a></li><li class="sect">YUI 3 Widgets - Examples</li><li class="item"><a title="Overlay - Functional Examples" href="../../examples/overlay/index.html">Overlay</a></li><li class="item"><a title="Slider - Functional Examples" href="../../examples/slider/index.html">Slider</a></li><li class="sect">YUI 3 Node Plugins - Examples</li><li class="item"><a title="FocusManager Node Plugin - Functional Examples" href="../../examples/node-focusmanager/index.html">FocusManager Node Plugin</a></li><li class="item"><a title="MenuNav Node Plugin - Functional Examples" href="../../examples/node-menunav/index.html">MenuNav Node Plugin</a></li><li class="sect">YUI 3 CSS - Examples</li><li class="item"><a title="YUI CSS Reset - Functional Examples" href="../../examples/cssreset/index.html">CSS Reset</a></li><li class="item"><a title="YUI Fonts - Functional Examples" href="../../examples/cssfonts/index.html">CSS Fonts</a></li><li class="item"><a title="YUI Base - Functional Examples" href="../../examples/cssbase/index.html">CSS Base</a></li><li class="sect">YUI 3 Developer Tools - Examples</li><li class="item"><a title="Console - Functional Examples" href="../../examples/console/index.html">Console</a></li><li class="item"><a title="Profiler - Functional Examples" href="../../examples/profiler/index.html">Profiler</a></li><li class="item"><a title="Test - Functional Examples" href="../../examples/test/index.html">Test</a></li><li class="sect">The YUI Community</li><li class="item"><a title="The Yahoo! User Interface Blog" href="http://yuiblog.com">YUI Blog (external)</a></li><li class="item"><a title="The Yahoo! Group YDN-JavaScript hosts the YUI community forum" href="http://tech.groups.yahoo.com/group/ydn-javascript/">YUI Forum (external)</a></li><li class="item"><a title="The Yahoo! Group yui3 is dedicated to the 3.x branch of the Yahoo! User Interface (YUI) Library." href="http://tech.groups.yahoo.com/group/yui3/">YUI 3 Forum (external)</a></li><li class="item"><a title="YUI is used by Yahoo! and by hundreds of other sites, including many you know and love." href="/yui/poweredby/">YUI Sightings (external)</a></li><li class="item"><a title="Videos and podcasts from the YUI Team and from the Yahoo! frontend engineering community." href="http://developer.yahoo.com/yui/theater/">YUI Theater (external)</a></li><li class="sect">YUI Articles on the YUI Website</li><li class="item"><a title="Answers to Frequently Asked Questions about the YUI Library" href="http://developer.yahoo.com/yui/articles/faq/">YUI FAQ (external)</a></li><li class="item"><a title="Yahoo!'s philosophy of Graded Browser Support" href="http://developer.yahoo.com/yui/articles/gbs/">Graded Browser Support (external)</a></li><li class="item"><a title="Reporting Bugs and Making Feature Requests for YUI Components" href="http://developer.yahoo.com/yui/articles/reportingbugs/">Bug Reports/Feature Requests (external)</a></li><li class="item"><a title="Serve YUI source files from Yahoo! -- free, fast, and simple" href="http://developer.yahoo.com/yui/3/articles/hosting/">Serving YUI Files from Yahoo! (external)</a></li></ul> |
|
|
222 |
</div> |
|
|
223 |
</div> |
|
|
224 |
</div><!--closes bd--> |
|
|
225 |
|
|
|
226 |
<div id="ft"> |
|
|
227 |
<p class="first">Copyright © 2009 Yahoo! Inc. All rights reserved.</p> |
|
|
228 |
<p><a href="http://privacy.yahoo.com/privacy/us/devel/index.html">Privacy Policy</a> - |
|
|
229 |
<a href="http://docs.yahoo.com/info/terms/">Terms of Service</a> - |
|
|
230 |
<a href="http://docs.yahoo.com/info/copyright/copyright.html">Copyright Policy</a> - |
|
|
231 |
<a href="http://careers.yahoo.com/">Job Openings</a></p> |
|
|
232 |
</div> |
|
|
233 |
</div> |
|
|
234 |
<script src="../../assets/dpSyntaxHighlighter.js"></script> |
|
|
235 |
<script language="javascript"> |
|
|
236 |
dp.SyntaxHighlighter.HighlightAll('code'); |
|
|
237 |
</script> |
|
|
238 |
</body> |
|
|
239 |
</html> |