source: cows/branches/cows-vis/cows/pylons/project_templates/cows_server/+package+/public/js/yui/examples/event/event-simple.html @ 5259

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/branches/cows-vis/cows/pylons/project_templates/cows_server/+package+/public/js/yui/examples/event/event-simple.html@5259
Revision 5259, 15.3 KB checked in by domlowe, 11 years ago (diff)

adding yui javascript

Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2<html>
3<head>
4        <title>YUI Library Examples: Event Utility: Simple DOM Events</title>
5    <meta http-equiv="content-type" content="text/html; charset=utf-8">
6        <link rel="stylesheet" type="text/css" href="../../assets/yui.css" >
7
8<style>
9    /*Supplemental CSS for the YUI distribution*/
10    #custom-doc { width: 95%; min-width: 950px; }
11    #pagetitle {background-image: url(../../assets/bg_hd.gif);}
12    #pagetitle h1 {background-image: url(../../assets/title_h_bg.gif);}
13</style>
14
15<link rel="stylesheet" type="text/css" href="../../assets/dpSyntaxHighlighter.css">
16<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" />
17<script type="text/javascript" src="../../build/yui/yui-min.js"></script>
18<style type="text/css">
19#container {background-color:#00CCFF; border:1px dotted black; padding:1em; cursor:pointer;}
20</style>
21</head>
22<body id="yahoo-com" class=" yui-skin-sam">
23<div id="custom-doc" class="yui-t2">
24<div id="hd">
25        <div id="ygunav">
26                <p>
27            <em>
28                <a href="http://developer.yahoo.com/yui/3/">YUI 3.x Home</a> <i> - </i> 
29            </em>
30                </p>
31                <form action="http://search.yahoo.com/search" id="sitesearchform">
32            <input name="vs" type="hidden" value="developer.yahoo.com">
33            <input name="vs" type="hidden" value="yuiblog.com">
34                    <div id="sitesearch">
35                        <label for="searchinput">Site Search (YDN &amp; YUIBlog): </label>
36                            <input type="text" id="searchinput" name="p">
37                            <input type="submit" value="Search" id="searchsubmit" class="ygbt">
38                    </div>
39                </form>
40    </div>
41        <div id="ygma"><a href="../../"><img src="../../assets/logo.gif"  border="0" width="200" height="93"></a></div>
42        <div id="pagetitle"><h1>YUI Library Examples: Event Utility: Simple DOM Events</h1></div>
43</div>
44<div id="bd">
45
46        <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>
47
48        <div id="yui-main">
49                <div class="yui-b">
50                  <div class="yui-ge">
51                          <div class="yui-u first example" id="main">
52
53        <h2>Event Utility: Simple DOM Events</h2>
54
55        <div id="example" class="promo">
56        <p>
57        <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>
58
59<p>Event Utility is used here to do two things:</p>
60
61<ol>
62        <li>Attach the <code>click</code> event handler to the blue box;</li>
63        <li>Attach an event handler to the second <code>&lt;a&gt;</code> element that uses <code>preventDefault()</code> to prevent the link, when clicked, from navigating to a new page. </li>
64
65</ol>   </p>   
66
67        <div class="module example-container ">
68                        <div class="hd exampleHd">
69                        <p class="newWindowButton yui-skin-sam">
70                <a href="event-simple_clean.html" target="_blank">View example in new window.</a>
71            </p>
72                </div>          <div id="example-canvas" class="bd">
73
74               
75        <!--BEGIN SOURCE CODE FOR EXAMPLE =============================== -->
76       
77       
78   
79<script language="javascript">
80
81YUI({base:"../../build/", timeout: 10000}).use("node",
82function(Y) {
83       
84    //A function that pops up a "Hello World" alert:
85    var helloWorld = function(e) {
86        Y.log("helloWorld function firing.", "info", "example");
87        alert("Hello World!");
88    }
89
90    //subscribe the helloWorld function as an event
91    //handler for the click event on the container
92    //div:
93    Y.on("click", helloWorld, "#container");
94
95    //A function that pops up an alert and
96    //prevents the default behavior of the event
97    //for which it is a handler:
98    var interceptLink = function(e) {
99        e.preventDefault();
100        Y.log("You clicked on the second YUI link.", "info", "example");
101        alert("You clicked on the second YUI link.  Because *preventDefault* was called, this link will not navigate away from the page.");
102    }
103
104    //subscribe our interceptLink function
105    //as a click handler for the second anchor
106    //element:
107    Y.on("click", interceptLink, "#secondA");
108
109    //log message to indicate that the example is ready:
110    Y.log("When you begin interacting with the example at left, you'll see log messages appear here.", "info", "example");
111});
112
113</script>
114
115<div id="container">
116<p>Click for Hello World alert.</p>
117</div>
118        <p><a href="http://developer.yahoo.com/yui" id="firstA">The YUI Library. (Link navigates away from page.)</a></p>
119
120        <p><a href="http://developer.yahoo.com/yui" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a></p>
121       
122        <!--END SOURCE CODE FOR EXAMPLE =============================== -->
123       
124               
125                </div>
126        </div>                 
127        </div>
128               
129        <h2 class="first">Making Use of the Event Utility's Basic Features</h2>
130
131<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>
132
133<p>To illustrate event handling syntax, we'll create a <code>&lt;div&gt;</code> and pop up an alert message when that <code>&lt;div&gt;</code> is clicked on.  Begin with the style and markup necessary to make your element visible:</p>
134
135<textarea name="code" class="HTML" cols="60" rows="1"><style type="text/css">
136#container {background-color:#00CCFF; border:1px dotted black; padding:1em; cursor:pointer;}
137</style>
138
139<div id="container">
140  <p>Click for Hello World alert.</p>
141</div></textarea>
142
143<p>Next, create a function that receives a single argument &mdash; the event object &mdash; and pops up an alert which says "Hello World!":</p>
144
145<textarea name="code" class="JScript" cols="60" rows="1">//A function that pops up a "Hello World" alert:
146var helloWorld = function(e) {
147        alert("Hello World!");
148}</textarea>
149
150<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>
151
152<textarea name="code" class="JScript" cols="60" rows="1">//subscribe the helloWorld function as an event
153//handler for the click event on the container
154//div:
155Y.on("click", helloWorld, "#container");
156</textarea>
157
158<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>
159
160<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>
161
162<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>
163<p><a href="http://developer.yahoo.com/yui" id="secondA">The YUI Library. (Link's default behavior is suppressed.)</a></p></textarea>
164
165<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 &mdash; 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>
166
167<textarea name="code" class="JScript" cols="60" rows="1">//A function that pops up an alert and
168//prevents the default behavior of the event
169//for which it is a handler:
170var interceptLink = function(e) {
171        e.preventDefault();
172        Y.log("You clicked on the second YUI link.", "info", "example");
173        alert("You clicked on the second YUI link.  Because *preventDefault* was called, this link will not navigate away from the page.");
174}
175
176//subscribe our interceptLink function
177//as a click handler for the second anchor
178//element:
179Y.on("click", interceptLink, "#secondA");</textarea>
180
181<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>
182                                </div>
183                                <div class="yui-u sidebar">
184                                       
185                               
186                                        <div id="examples" class="mod box4">
187                        <div class="hd">
188                                                <h4>
189    Event Utility Examples:</h4>
190                        </div>
191                                                <div class="bd">
192                                                        <ul>
193                                                                <li class='selected'><a href='../event/event-simple.html'>Simple DOM Events</a></li><li><a href='../event/event-timing.html'>Using onAvailable, onContentReady, and event:ready</a></li><li><a href='../event/event-ce.html'>Using Custom Events</a></li><li><a href='../attribute/attribute-event.html'>Change Events (included with examples for Attribute Utility)</a></li><li><a href='../attribute/attribute-getset.html'>Custom Getters, Setters and Validators (included with examples for Attribute Utility)</a></li>                                                   </ul>
194                                                </div>
195                                        </div>
196                                       
197                                        <div class="mod box4">
198                        <div class="hd">
199                                                <h4>More Event Utility Resources:</h4>
200                        </div>
201                        <div class="bd">
202                                                <ul>
203                                                        <!-- <li><a href="http://developer.yahoo.com/yui/event/">User's Guide</a> (external)</li> -->
204                                                <li><a href="../../api/module_event.html">API Documentation</a></li>
205</ul>
206                        </div>
207                                        </div>
208                          </div>
209                </div>
210               
211                </div>
212        </div>
213
214
215<div class="yui-b toc3" id="tocWrapper">
216<!-- TABLE OF CONTENTS -->
217<div id="toc">
218       
219<ul>
220<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="Base - Functional Examples" href="../../examples/base/index.html">Base</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="Cookie - Functional Examples" href="../../examples/cookie/index.html">Cookie</a></li><li class="item"><a title="Drag &amp; Drop - Functional Examples" href="../../examples/dd/index.html">Drag &amp; Drop</a></li><li class="item"><a title="Get - Functional Examples" href="../../examples/get/index.html">Get</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="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 Grids - Functional Examples" href="../../examples/cssgrids/index.html">CSS Grids</a></li><li class="item"><a title="YUI Base - Functional Examples" href="../../examples/cssbase/index.html">CSS Base</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>
221</div>
222</div>
223        </div><!--closes bd-->
224
225        <div id="ft">
226        <p class="first">Copyright &copy; 2008 Yahoo! Inc. All rights reserved.</p>
227        <p><a href="http://privacy.yahoo.com/privacy/us/devel/index.html">Privacy Policy</a> -
228            <a href="http://docs.yahoo.com/info/terms/">Terms of Service</a> -
229            <a href="http://docs.yahoo.com/info/copyright/copyright.html">Copyright Policy</a> -
230            <a href="http://careers.yahoo.com/">Job Openings</a></p>
231        </div>
232</div>
233<script src="../../assets/dpSyntaxHighlighter.js"></script>
234<script language="javascript"> 
235dp.SyntaxHighlighter.HighlightAll('code'); 
236</script>
237</body>
238</html>
239<!-- delightfuture.corp.yahoo.com uncompressed/chunked Wed Aug 13 16:45:32 PDT 2008 -->
Note: See TracBrowser for help on using the repository browser.