This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-notes.html
793 lines (682 loc) · 26.3 KB
/
jquery-notes.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<link href="css/prettify.css" type="text/css" rel="stylesheet" />
<link href="css/desert.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="css/style.css" rel="stylesheet" />
<script type="text/javascript" src="js/prettify.js"></script>
</head>
<body onload="prettyPrint()">
<div id="notes_header">
<span>15-237: Building Cross-Platform Mobile Web Apps</span>
<h1>jQuery</h1>
</div>
<section>
<h2>What is jQuery?</h2>
<section>
<p>jQuery is a popular javascript library that makes <strong>DOM manipulation</strong>, <strong>event handling</strong>, <strong>animation</strong>, and <strong>AJAX</strong> much easier. The goal behind the library is to allow you to do more by writing less.</p>
</section>
</section>
<section>
<h2>Installing jQuery</h2>
<section>
<p>To install jQuery, go to <a href="http://jquery.com/">their website</a>, download the latest production version (1.8.1 at the time of this writing) and put it somewhere in your project directory, preferably in a folder called "js" at the root of your project. Then, link it in your html file with:</p>
<pre class="prettyprint lang-html">
<script src="js/jquery.js"></script></pre>
</section>
</section>
<section>
<h2>Fiddling with jQuery</h2>
<section>
<p>We can actually enable jQuery in jsfiddle by selecting it in the second dropdown box on the left, under "Choose Framework". This is super useful!</p>
</section>
</section>
<section>
<h2>The "jQuery" Function ($)</h2>
<section>
<p>The jQuery function is what allows you to take advantage of all those awesome jQuery features. The jQuery function is aliased to the <span class="code">$</span> symbol, which makes accessing it super quick and easy. Here are some examples</p>
</section>
<h3>Selecting DOM Elements using jQuery</h3>
<section>
<p>You can pass the jQuery function a CSS selector (as a string) to get back a jQuery object representing the matching elements.</p>
<pre class="prettyprint lang-js">
var items = $("a");
/* Returns a jQuery object with all the <a> tags on the page */</pre><br>
<pre class="prettyprint lang-js">
var items = $("#container p");
/* Returns a jQuery object with all the <p> inside the element with id="container" */</pre>
<p>By default, the jQuery object searches the entire DOM. If you want to restrict your search, you can pass the jQuery function a context as a second argument, like this:</p>
<pre class="prettyprint lang-js">
var items = $("li", "ul#special");
/* Returns a jQuery object with all the <li> tags found within the <ul> tag with id="special" */</pre>
<p>Functionally, that last example is the same as:</p>
<pre class="prettyprint lang-js">
var items = $("ul#special").find("li");</pre>
</section>
<h3>jQuery Objects</h3>
<section>
<p>Wait a minute, what's a jQuery Object? It's just a wrapper object around one or more DOM nodes, which gives them extended functionality. Above, we saw that a jQuery object can be made by passing a selector to the jQuery function. If, for some reason, you already have the plain non-jQuery DOM node, you could pass this to the jQuery function and get back a jQuery object:</p>
<pre class="prettyprint lang-js">
var myBoringNode = document.getElementById("myID");
var myAwesomeJqueryObject = $(myBoringNode);</pre>
<p>If you wanted, you could even pass a jQuery object to the jQuery function, and you'd get back...a clone of that jQuery object:</p>
<pre class="prettyprint lang-js">
var myAwesomejQueryObject = $(document.getElementById("myID"));
var aCloneOfThatObject = $(myAwesomeJQueryObject);</pre>
<p>Note that a jQuery object is something created by the jQuery function. Really though, since functions are objects in javascript, they could technically both be called the "jQuery" object. But don't do that. When we say "jQuery function", we mean <span class="code">$()</span>. When we say "jQuery object", we mean something <span class="code">returned by $()</span>.</p>
</section>
<h3>Making HTML Elements with jQuery</h3>
<section>
<p>You can also use the jQuery function to create new HTML elements, by passing them a string of HTML, like this:</p>
<pre class="prettyprint lang-js">
var newNode = $("<div></div>");
// Returns a jQuery Object wrapped <div> node</pre>
<p>The shorthand for this operation is:</p>
<pre class="prettyprint lang-js">
var newNode = $("<div/>");
// Does the same as above</pre>
</section>
<h3>Testing Selections</h3>
<section>
<p>Say we want to check if our jQuery selection returned anything. Here's an example of something that <strong>won't</strong> work:</p>
<pre class="prettyprint lang-js">
if ( $("#container") ){
// code in here will run even if the selector found nothing.
}</pre>
<p>Even if it finds nothing, the jQuery function still returns a jQuery object. Since objects are truthy, the above method won't work. Instead, we can use the <span class="code">length</span> property of the returned jQuery object to see what it found.</p>
<pre class="prettyprint lang-js">
if ( $("#container").length > 0){
// code in here will only run if the selector returned at least 1 thing
}</pre>
</section>
</section>
<section>
<h2>Operating on jQuery Objects</h2>
<section>
<p>Making something into a jQuery object allows us to do lots of things very easily. Many of the following functions fall into two categories: <span class="code">getters</span>, and <span class="code">setters</span>. In almost all cases, getter functions will only operate on the first in the set of matched elements, while setters will operate on all elemenets in the set of matched elements.</p>
</section>
<h3>Working with Classes</h3>
<section>
<pre class="prettyprint lang-js">
var foo = $("ul a");
foo.addClass("wahoo");
foo.hasClass("wahoo"); // true
foo.removeClass("wahoo");
foo.toggleClass("wahoo");
// adds the class if it's not present,
// removes it if it is</pre>
</section>
<h3>Working with HTML attributes</h3>
<section>
<pre class="prettyprint lang-js">
var myInput = $("#myInput");
var inputType = myInput.attr("type"); // Get the input's "type"
if(inputType == "text"){
// do something
} else {
// do something else
}</pre>
<pre class="prettyprint lang-js">
var images = $("img");
images.attr("title", "Carpe Diem!")
// set the title of all images to "Carpe Diem!"</pre>
</section>
<h3>Working with CSS</h3>
<section>
<pre class="prettyprint lang-js">
var myContainer = $("#container");
var bg_color = myContainer.css("background-color"); // returns an rgb string, NOT hex;
if (bg_color == "rgb(0, 0, 0)"){
myContainer.css("color", "white"); // set the css color
} else {
myContainer.css("color", "#000"); // set the css color
}
/* The css() function applies an inline style, and does NOT
* affect any CSS rules in a <style> tag or external
* stylesheet!
*/</pre>
</section>
<h3>Working with HTML</h3>
<section>
<p>Assume we have the following HTML:</p>
<pre class="prettyprint lang-html">
<div id="container">
<p>15-237 is awesome!</p>
</div></pre>
<p>Here's the JS/jQuery:</p>
<pre class="prettyprint lang-js">
var myContainer = $("#container");
var innerHTML = myContainer.html(); // returns "<p>15-237 is awesome!</p>"
myContainer.prepend("<p>I'm a prepended paragraph!</p>");
// inserts into DOM as first child of myContainer
myContainer.append("<p>I'm an appended paragraph!</p>");
// inserts into DOM as last child of myContainer
var myParagraph = $("p", myContainer); // selects all paragraphs within myContainer
myParagraph.append(" Wahoo!"); // appends " Wahoo!" to all of them
var myHTML = myParagraph.html(); // gets the innerHTML of the FIRST element in the set
alert(myHTML); // "I'm a prepended paragraph! Wahoo!"
var myText = myParagraph.text(); // gets the text of ALL elements in the set, even child nodes
alert(myText);
// "I'm a prepended paragraph! Wahoo! 15-237 is awesome! Wahoo! I'm an appended paragraph! Wahoo!"
var newLink = $("<a href='www.cs.cmu.edu/15-237'>15-237 course website</a>");
myContainer.after(newLink); // insert the newLink after the selected element</pre>
<p>
<a href="http://jsfiddle.net/dillongrove/jkQaM/#base">JSFiddle</a>
</p>
</section>
<h3>DOM Traversal / Filtering Selections</h3>
<section>
<p>Assume the following HTML:</p>
<pre class="prettyprint lang-html">
<div class="foo" id="first">
<div class="foo" id="second">
<div class="bar" id="third">
<div class="woo" id="fourth">
</div>
<div class="woo" id="fifth">
</div>
</div>
</div>
</div></pre>
<p>Here's the JS/jQuery:</p>
<pre class="prettyprint lang-js">
var first = $("#first");
var x = first.children(".woo"); // travels 1 level down
alert(x.length); // 0
var y = first.find(".woo"); // travels multiple levels down
alert(y.length); // 2
var fourth = $("#fourth");
var a = fourth.closest(".foo"); // travels up until it finds something that matches
alert(a.attr("id")); // second
var b = fourth.next(); // gets the next sibling
alert(b.attr("id")); //fifth
var c = fourth.next(".foo"); // can filter with selector
alert(c.length) // 0 (nothing matches filter)
var d = fourth.parents();
alert(d.length); // 5 (includes body and html, not seen above in jsfiddle)
var e = fourth.parents(".foo");
alert(e.length); // 2 (only 2 parents with class of foo)
var f = e.filter("#first"); // filter the selected elements
alert(f.length) // 1</pre>
<p>
<a href="http://jsfiddle.net/dillongrove/k68Ce/#base">JSFiddle</a>
</p>
</section>
<h3>Explicit Iteration</h3>
<section>
<p>Iterate over a collection of selected elements using the <span class="code">.each()</span> function.</p>
<pre class="prettyprint lang-html">
<ol>
<li>Eat</li>
<li>Sleep</li>
<li>Code</li>
<li>Repeat</li>
</ol> </pre><br>
<pre class="prettyprint lang-js">
$("li").each(function(){
var text = $(this).text();
alert(text);
// use "return false" to stop iteration prematurely
});</pre>
<p>
<a href="http://jsfiddle.net/dillongrove/KNUCQ/#base">JSFiddle</a>
</p>
<p>Inside the body of the <span class="code">each</span> function, you'll notice the keyword <span class="code">this</span>. Here, <span class="code">this</span> refers to the plain DOM node you're currently operating on, and <strong>NOT</strong> a jQuery object. If you need the node to be a jQuery object, simpy wrap it in the jQuery function, using <span class="code">$(this)</span>, as shown in the example above.</p>
</section>
<h3>Chaining</h3>
<section>
<p>We already know that DOM traversal functions (like find or children) return a jQuery object. However, even jQuery functions not associated with filtering selections or DOM traversal return a jQuery object. In this case, they return the jQuery object that they were called on. This is a very useful construct, and allows us to "chain" together multiple jQuery functions like so:</p>
<pre class="prettyprint lang-js">
$("myNode").addClass("basil").attr("id", "parsely").css("font-size", "24px");</pre>
<p>Read more about chaining <a href="http://tobiasahlin.com/blog/quick-guide-chaining-in-jquery/">here</a>.</p>
</section>
<h3>Miscellaneous</h3>
<ul class="no_bullet">
<li>
<strong>Get the computed height of an element</strong>
<br>
<pre class="prettyprint lang-js">
var theHeight = $("#container").height();
// Returns the actual computed height of the element, regardless of what CSS specifies</pre>
</li>
<li>
<strong>Get the position of an element relative to the document</strong>
<br>
<pre class="prettyprint lang-js">
var theOffset = $("#container").offset();
// Returns an object containing "left" and "top" offsets
var x = theOffset.left;
var y = theOffset.top;</pre>
</li>
<li>
<strong>Get the vertical scroll amount</strong>
<br>
<pre class="prettyprint lang-js">
var scrollAmount = $("body").scrollTop();
// Returns the number of pixels out of view at the top due to scrolling
// Can target other scrollable elements besides just the page</pre>
</li>
</ul>
</section>
<section>
<h2>Events</h2>
<p>jQuery provides an easy interface for working with events.</p>
<h3>The Document Ready Event</h3>
<section>
<pre class="prettyprint lang-js">
$(document).ready(function(){
// code goes here!
});</pre>
<p>This event is fired once the DOM has finished loading. In most cases, whatever javascript or jQuery we're writing needs to the DOM to be fully loaded in order to work. Otherwise, the elements we plan to operate on won't exist yet.</p>
<p>The <span class="code">.ready()</span> event is similar to the normal DOM <span class="code">load</span> event we've already been using, with one key difference. The <span class="code">.ready()</span> event is fired as soon as the DOM is loaded, while the <span class="code">load</span> event waits until all assets (like css or images) have been fully loaded too.</p>
</section>
<h3>The Click Event - A Quick Example</h3>
<section>
<pre class="prettyprint lang-html">
<div id="container">
<a href="www.google.com">Google</a><br>
<a href="www.apple.com">Apple</a><br>
<a href="www.microsoft.com">Microsoft</a><br>
</div></pre><br>
<pre class="prettyprint lang-js">
$(document).ready(function(){
$("a").click(function(event){
var foo = this; // refers to the DOM node that called the event
var bar = $(this) // same as above, but wrapped in a jQuery object
// good so far?
var baz = event.target;
// the element where the event initiated
// may not be the same as the element handling the event!
bar.css("color", "red");
event.preventDefault();
// prevents the default action (going to the link) from happening
});
$("#container").click(function(event){
var currentThis = $(this).attr("id");
var eventTarget = $(event.target);
alert("current this: " + currentThis);
alert("event target: " + eventTarget.text());
// no default action for clicking a div, no preventDefault needed
});
});</pre>
` <p>
<a href="http://jsfiddle.net/dillongrove/v3TNz/#base">JSFiddle</a>
</p>
</section>
<h3>Other Events</h3>
<section>
<p>There are many, many other events. Here's a small sample:</p>
<ul>
<li><span class="code">.dblclick()</span></li>
<li><span class="code">.focus()</span></li>
<li><span class="code">.hover()</span></li>
<li><span class="code">.mouseenter()</span></li>
<li><span class="code">.mouseleave()</span></li>
<li><span class="code">.mouseup()</span></li>
<li><span class="code">.mousedown()</span></li>
<li><span class="code">.mousemove()</span></li>
</ul>
<p>By the way, in most cases, calling one of the functions above without passing it a handler function simply triggers that event on the specified elements.</p>
</section>
<h3>More On Events</h3>
<section>
<p>The jQuery site has an awesome tutorial on events that explains some of the intricacies involved <a href="http://docs.jquery.com/Tutorials:AJAX_and_Events">here</a>.</p>
See the full documentation for jQuery Events <a href="http://api.jquery.com/category/events/">here</a>.
</section>
</section>
<section>
<h2>Effects</h2>
<section>
<p>jQuery provides quick access to some simple animations through it's effects funtions.</p>
</section>
<h3>Example 1</h3>
<section>
<strong>The HTML</strong><br>
<pre class="prettyprint lang-html">
<div id="first"></div>
<div id="second"></div>
<div id="third"></div>
<a href="#">Reset</a></pre><br>
<strong>The CSS</strong><br>
<pre class="prettyprint lang-css">
body { margin: 10px; }
div { width: 100px; height: 100px; margin-bottom: 10px; }
#first { background-color: blue }
#second { background-color: red }
#third { background-color: green }</pre><br>
<strong>The JS + jQuery</strong><br>
<pre class="prettyprint lang-js">
$(document).ready(function(){
$("#first").click(function(){
$(this).hide();
});
$("#second").click(function(){
$(this).fadeOut(1000);
});
$("#third").click(function(){
$(this).slideUp("slow")
});
$("a").click(function(){
$("#first").show(0, function(){
$("#second").fadeIn(1000, function(){
$("#third").slideDown(1000);
});
});
});
});</pre>
<p>
<a href="http://jsfiddle.net/dillongrove/83scA/">JSFiddle</a>
</p>
</section>
<h3>Example 2 (jQuery compared with Javascript)</h3>
<section>
<strong>The HTML</strong>
<br>
<pre class="prettyprint lang-html">
<html>
<body>
<div class='square blue'></div>
<div class='square blue classNameWith_blue_butShouldNotToggle'></div>
</body>
</html></pre>
<br>
<strong>The CSS</strong>
<br>
<pre class="prettyprint lang-css">
.square {
width: 5em;
height: 5em;
margin: 2em;
border: 1px solid black;
}
.blue {
background-color: blue;
}</pre>
<br>
<strong>The jQuery/JS</strong>
<br>
<pre class="prettyprint lang-js">
//toggling a class
//===========================
var useJQuery = false
//jquery
//===========================
if (useJQuery) {
setInterval(function() {
$('.square').toggleClass('blue');
}, 1000);
}
//vanilla JS
//===========================
var toggleClass = function(dom, targetName) {
var classNames = dom.className.split(' ');
for (var i = 0; i < classNames.length; i++) {
if (classNames[i] === targetName) {
dom.className = ' ' + dom.className + ' ';
dom.className = dom.className.replace(' ' + targetName + ' ', ' ');
dom.className = dom.className.substring(1, dom.className.length - 1);
hadClass = true;
return;
}
}
dom.className += ' ' + targetName;
}
if (!useJQuery) {
setInterval(function() {
var squares = document.getElementsByClassName('square');
for (var i = 0; i < squares.length; i++) {
toggleClass(squares[i], 'blue');
}
}, 1000);
}</pre>
<br>
<p><a href="http://jsfiddle.net/dillongrove/xZhrU/#base">JSFiddle</a></p>
</section>
<h3>Example 3</h3>
<section>
<strong>The HTML</strong>
<br>
<pre class="prettyprint lang-html">
<div class="accordion">
<h1>This gives the accordion effect</h1>
<div class="inside">
<p>This stuff is inside the accordion</p>
<p>You can't see it unless you unfold the accordion</p>
</div>
</div>
<div class="accordion">
<h1>Another one</h1>
<div class="inside">
<p>This stuff is inside the accordion</p>
<p>Just another example!</p>
</div>
</div></pre>
<br>
<strong>The CSS</strong>
<br>
<pre class="prettyprint lang-css">
.accordion {
width: 80%;
background-color: #E0E0E0;
margin: 10px 5px;
}
.accordion h1{
font-weight: bold;
cursor: pointer;
background-color: #F8F8F8;
margin-bottom: 10px;
padding: 10px;
}
.accordion p {
padding: 10px;
}
.inside {
display: none;
}</pre>
<br>
<strong>The jQuery/JS</strong>
<br>
<pre class="prettyprint lang-js">
$(document).ready(function() {
// ties an event to .accordion click
$('.accordion').click(function() {
// toggles the sliding of elements with .inside class
// ONLY those that are children to this
$(this).children('.inside').slideToggle();
});
});</pre>
<br>
<p><a href="http://jsfiddle.net/dillongrove/acbFj/#base">JSFiddle</a></p>
</section>
<h3>Example 4</h3>
<section>
<strong>The HTML</strong>
<br>
<pre class="prettyprint lang-html">
<div class="alert">
<h1>Warning</h1>
<p>There was a configuration error</p>
<span class="close">x</span>
</div>
<div class="alert">
<h1>Warning</h1>
<p>There was an unknown error</p>
<span class="close">x</span>
</div>
<div class="alert">
<h1>Warning</h1>
<p>There were random errors</p>
<span class="close">x</span>
</div>
​</pre>
<br>
<strong>The CSS</strong>
<br>
<pre class="prettyprint lang-css">
body {
margin: 20px;
font-family: 'Lucida Grande', arial, sans-serif;
}
.alert {
background-color:#FFC0C0;
color: #662020;
padding: 15px;
border-radius: 5px;
margin-bottom: 20px;
}
.alert h1 {
display: inline;
font-weight: bold;
margin-right: 10px;
}
/* to keep it on the same line */
.alert p {
display: inline;
}
.alert .close {
color: #441010;
float: right;
cursor: pointer;
}
/* mouseover pseudo-class */
.alert .close:hover {
color: #A08080;
}</pre>
<br>
<strong>The jQuery/JS</strong>
<br>
<pre class="prettyprint lang-js">
$(document).ready(function() {
// tie click event to all .close that are children of .alert
$('.alert > .close').click(function() {
// fade out the parent (the .alert) in 500ms
$(this).parent().fadeOut(500);
});
});</pre>
<br>
<p><a href="http://jsfiddle.net/dillongrove/3DhMz/#base">JSFiddle</a></p>
</section>
</section>
<section>
<h2>Custom Animations</h2>
<h3>A Quick Example</h3>
<section>
<strong>The HTML</strong>
<br>
<pre class="prettyprint">
<div id="test">Content</div></pre><br>
<strong>The CSS</strong>
<br>
<pre class="prettyprint">
#test {
display: block;
width: 200px;
height: 200px;
background-color: red;
}</pre><br>
<strong>The jQuery/JS</strong>
<br>
<pre class="prettyprint">
$(document).ready(function() {
$('#test').click(function() {
$(this).animate({
height: '300px',
width: '300px',
margin: '100px'
}, 1000, function() {
$(this).html('Done animating!');
});
});
});</pre><br>
<a href="http://jsfiddle.net/dillongrove/byM2Z/#base">JSFiddle</a>
</section>
</section>
<section>
<h2>AJAX</h2>
<h3>An AJAX Example: JS vs jQuery</h3>
<section>
<strong>The HTML</strong><br>
<pre class="prettyprint lang-html">
<!DOCTYPE html>
<html>
<body>
<h2>Carnegie Mellon's Twitter</h2>
<div id="content">Loading...</div>
</body>
<html></pre>
<br>
<strong>The jQuery/JS</strong><br>
<pre class="prettyprint linenums lang-js">
var useJQuery = false;
var url = 'http://twitter.com/status/user_timeline/CarnegieMellon.json';
//jquery
//====================================
var jqueryCallback = function(items) {
var content = $("#content");
content.empty();
for (var i = 0; i < items.length; i++) {
var div = $("<div>");
div.text(items[i].text);
content.append(div);
content.append($("<br>"));
}
}
if (useJQuery) {
$.ajax({
url: url + '?count=10&callback=?',
dataType: 'json',
success: jqueryCallback
});
}
//vanilla js
//===================================
var vanillaJSCallback = function(items) {
var content = document.getElementById('content');
content.innerHTML = '';
for (var i = 0; i < items.length; i++) {
var div = document.createElement('div');
div.innerText = items[i].text;
content.appendChild(div);
content.appendChild(document.createElement('br'));
}
}
function loadJS(url, callback) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onload = callback;
document.body.appendChild(script);
}
function getJSON(url, callbackKey, callback, extras) {
if (window.jsonpCallbackId === undefined) window.jsonpCallbackId = 0;
window.jsonpCallbackId++;
var callbackName = 'jsonpcallback_' + window.jsonpCallbackId;
window[callbackName] = function(json) {
delete window[callbackName];
callback(json);
}
if (extras !== undefined) loadJS(url + '?' + callbackKey + '=' + callbackName + '&' + extras);
else loadJS(url + '?' + callbackKey + '=' + callbackName);
}
if (!useJQuery) {
getJSON(url, 'callback', vanillaJSCallback, 'count=10');
}</pre>
<br>
<p>
<a href="http://jsfiddle.net/dillongrove/nHzEd/#base">JSFiddle</a>
</p>
</section>
</section>
<section>
<h2>jQuery Documentation</h2>
<section>
<p>It's awesome and you should read it <a href="http://docs.jquery.com/Main_Page">here</a>. Look on the left side under "API reference"</p>
</section>
</section>
<footer>
<span>jQuery</span>15-237: Building Cross-Platform Mobile Web Apps
</footer>
</body>
</html>