-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
2030 lines (1855 loc) · 124 KB
/
index.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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--
Fixing These jQuery
by adam j. sontag
WTFPL License
If you want the cool kid fonts for this preso:
http://www.fontsquirrel.com/fontfacedemo/ChunkFive
http://www.fontsquirrel.com/fontfacedemo/CartoGothic-Std
This all refers to the Presentation slides or whatever.
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Copyright 2010 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Original slides: Marcin Wichary ([email protected])
Modifications: Ernest Delgado ([email protected])
Alex Russell ([email protected])
Brad Neuberg
-->
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge;chrome=1" />
<title>Fixing These jQuery | adam j. sontag </title>
<link href="css/fonts.css" rel="stylesheet" type="text/css" />
<link href="css/main.css" rel="stylesheet" type="text/css" />
<link href="css/syntax.css" rel="stylesheet" type="text/css" />
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/eggplant/jquery-ui.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="presentation">
<div id="presentation-counter"></div>
<div class="slides">
<div class="slide" id="intro">
<div class="bg">
<section class="middle landing">
<h1>Fixing These <span class='jqueryblue'>jQuery!</span><br/><span class='subtitle'>A Guide to Debugging</span></h1>
<h4>adam j. sontag</h4>
</section>
</div>
<div id="welcome">
<p>
<strong>Thanks for coming!</strong></p>
<p><em>Fixing These jQuery</em> is an HTML5 presentation designed to familiarise developers with basic approaches to debugging
jQuery and JavaScript code. It also introduces many of the common pitfalls most people encounter at some point on their jQuery journey.
It is always a work in progress!
</p>
<p>Use the left <span class='key'>←</span> and right <span class='key'>→</span> arrow keys or your mouse wheel to navigate.</p>
</div>
</div>
<div class="slide">
<header>
<h1>Who dat?</h1>
</header>
<section class="text">
<ul>
<li><a href="http://yayquery.com">yayQuery</a> Cohost!</li>
<li><a href="http://bocoup.com">Bocoup</a> Dev!</li>
<li>I've debugged a lot of my own jQuery problems</li>
<li>I've debugged a lot of other people's jQuery problems</li>
<li>I've seen <del>it all</del> a lot</li>
</ul>
</section>
</div>
<div class="slide">
<header>
<h1>jQuery is JavaScript</h1>
</header>
<section class="text">
<p>You <strong>will</strong> have to debug it.</p>
<pre class="brush: js; toolbar: false">
window.alert("Is Not A Debugger")</pre>
<p>Why?</p>
<iframe src="dontdothis.html" style='width:100%;height800px;' height='300'>Check Out My Awesome, Hand-Rolled Tooltip!</iframe>
</section>
</div>
<div class="slide">
<header>
<h1>JavaScript Debuggers Exist</h1>
<section>
<p class="quoteThing">And if you aren't using them, you are insane.</p>
</section>
</header>
</div>
<div class="slide" id='debugger-icons'>
<header>
<h1>Debuggers</h1>
</header>
<section class="text">
<figure>
<img src="images/firefox.png">
<img src="images/firebug.png">
<figcaption>
Firefox uses <a href="http://getfirebug.com">Firebug</a>
</figcaption>
<aside>
First, <a href="https://addons.mozilla.org/en-US/firefox/addon/1843/">install it</a><br/>
F12, or click the insect
</aside>
</figure>
<figure>
<img src="images/safari.png">
<img src="images/chrome.png">
<img src="images/webkit.png">
<figcaption>
Chrome and Safari use <a href="http://trac.webkit.org/wiki/WebInspector">WebKit Inspector</a>
</figcaption>
<aside>
Cmd-Opt-I / Ctrl-Shift-I
</aside>
</figure>
<figure>
<img src="images/opera.png">
<img src="images/dragonfly.png">
<figcaption>
Opera uses <a href="http://www.opera.com/dragonfly/">Dragonfly</a>
</figcaption>
<aside>
Cmd-Opt-I / Ctrl-Shift-I
</aside>
</figure>
<figure>
<img src="images/ie8-logo.png">
<img src="images/ie9-logo.png">
<figcaption>
IE8 and IE9 have a built-in debugger
</figcaption>
<aside>
F12
</aside>
</figure>
<figure>
<img src="images/ie6-logo.png">
<img src="images/visualstudio.png">
<figcaption>
IE6 and IE7 require you to use a host app to debug. Lame.
</figcaption>
<aside>
GLWTD
</aside>
</figure>
</section>
</div>
<div class="slide debugger-screenshots accordioned">
<header>
<h1>All Look Same</h1>
</header>
<section class="text">
<figure>
<img src="images/firebug_screenshot.png">
<figcaption>
Firebug
</figcaption>
</figure>
<figure>
<img src="images/webkit_inspector_screenshot.png">
<figcaption>
Webkit Inspector
</figcaption>
</figure>
<figure>
<img src="images/dragonfly_screenshot.png">
<figcaption>
Dragonfly
</figcaption>
</figure>
<figure>
<img src="images/ie_debugger.png">
<figcaption>
Internet Explorer 8
</figcaption>
</figure>
</section>
</div>
<div class="slide debugger-screenshots">
<header>
<h1>ZOMG!</h1>
</header>
<section class="text">
<figure>
<img src="images/it_shows_errors.png" id="it_shows_errors">
<figcaption>
The Debugger SHOWS YOU THE ERRORS!!!
</figcaption>
</figure>
<ul>
<li>A debugger will show you syntax errors and runtime exceptions</li>
<li><strong>Save time: Use your IDE or text editor to catch syntax errors (JSLint)</strong></li>
</ul>
</section>
</div>
<div class="slide">
<header>
<h1>Really Basic Errors</h1>
<h2>Let's get these out of the way</h2>
</header>
<section class="text muchSmallerCode" style="margin-top:163px">
<dl>
<dt>jQuery is not defined</dt>
<dd>You have not loaded jQuery correctly</dd>
<dd>Is the script included?</dd>
<dd>Is the path correct?</dd>
<dt>$ is not defined</dt>
<dd>jQuery is loaded, but the $ alias is not assigned to it.</dd>
<dd>Was <code>jQuery.noConflict()</code> run? (Wordpress does this by default.)</dd>
<dt>$.fn is not defined</dt>
<dd>Are you using another library that is taking the $ alias?</dd>
<dt>jQuery.fn.somePlugin is not defined</dt>
<dd>You have not loaded the plugin correctly</dd>
<dd>Is the script included?</dd>
<dd>Is the path correct?</dd>
<dd>Did you load the plugin <em>after</em> loading jQuery?</dd>
</dl>
</section>
</div>
<div class="slide">
<header>
<h1>Debuggin' <del>ain't</del> is easy</h1>
</header>
<section class="text">
<p>Instead of this ugly, useless ugly alert...</p>
<pre class="brush: js; toolbar: false; highlight:[6]">
$("ul li span").hover(function(e) {
var $t = $(this),
w = $t.width(),
o = $t.offset();
o.left = o.left + w;
alert(o);
span.text($t.text())
tip.offset(o).show();
});
</pre>
</section>
</div>
<div class="slide debugger-screenshots">
<header>
<h1>...Set a Breakpoint!</h1>
</header>
<section class="text">
<figure>
<img src="images/set_breakpoint.png">
<figcaption>
<ol>
<li>Select the script you want to debug</li>
<li>Click on the line number where you want to inspect further</li>
<li>An arrow or dot thingy will show up. That's a <a href="http://en.wikipedia.org/wiki/Breakpoint">breakpoint</a>!</li>
</ol>
</figcaption>
</figure>
</section>
</div>
<div class="slide">
<header>
<h1>The <code>debugger;</code> statement</h1>
</header>
<section class="text">
<p>Don't want to wait to set a breakpoint?</p>
<pre class="brush: js; toolbar: false; highlight:[6]">
$("ul li span").hover(function(e) {
var $t = $(this),
w = $t.width(),
o = $t.offset();
o.left = o.left + w;
debugger;
span.text($t.text())
tip.offset(o).show();
});
</pre>
<p>The <code>debugger;</code> statement lets you set a breakpoint programmatically.
</section>
</div>
<div class="slide debugger-screenshots">
<header>
<h1>In either case</h1>
</header>
<section class="text">
<figure>
<img src="images/reach_breakpoint.png">
<figcaption>
When the breakpoint is reached, the code will stop executing and the debugger will light up like a Christmas tree.
</figcaption>
</figure>
<p>And just like a Christmas tree, you can find all sorts of awesome stuff underneath it.</p>
</section>
</div>
<div class="slide">
<header>
<h1>Watch out! We're going <em>¡Local!</em></h1>
<h2>The Watch/Locals windows</h2>
</header>
<section class="text" style="margin-top:163px">
<figure>
<img src="images/wi_locals.png" style="width:40%;">
<img src="images/fb_watch.png" style="width:40%;">
<figcaption>
Chrome/Safari's Scope Variables; Firebug's Watch window
</figcaption>
</figure>
<ul>
<li>Determine the current value of variables.</li>
<li>Drilldown through objects - no more [Object object]!</li>
<li><strong>Watch:</strong> Keep track of arbitrary expressions</li>
</ul>
</section>
</div>
<div class="slide">
<header>
<h1>R U Steppin To Me Bro?</h1>
</header>
<section class="text">
<figure>
<img src="images/wi_controls.png" style="width:50%;display:block;margin:1em auto 0;">
<figcaption>
Continue - Step Over - Step Into - Step Out
</figcaption>
</figure>
<p>When execution is paused in a debugger, you can control it - just like a VCR!</p>
<dl>
<dt>Continue</dt>
<dd>Resume execution. Go to the next breakpoint, if it exists.</dd>
<dt>Step Over</dt>
<dd>Call the next function, pause again after it finishes. (Go to the next line of code.)</dd>
<dt>Step Into</dt>
<dd>Calls the next function on the line, pausing execution on its first line.</dd>
<dt>Step Out</dt>
<dd>Finish the current function call, pause on the line that invoked it.</dd>
</dl>
</section>
</div>
<div class="slide">
<header>
<h1><code>console.log</code> & friends</h1>
<h2>Pullin' out all the stops!</h2>
</header>
<section class="smallerCode text" style="margin-top:163px;">
<ul>
<li>Breakpoints are great, but are similar to alerts. How?</li>
<li>Wait for something to happen, and pause when it does</li>
<li>The Console and its API let you debug arbitrary code and values</li>
</ul>
<pre class="brush: js; toolbar: false">
$('#testElement')
.click({"foo":"bar"},function(e) {
console.info("The button has been pressed");
console.dir(e.data);
// You can pass an unlimited list of arguments
console.log(this,e);
$(this).append('<div>Hello</div>');
}); </pre>
</section>
</div>
<div class="slide">
<header class="punny-picture">
<h1>Console loggin'</h1>
<a href="http://www.flickr.com/photos/69393358@N00/1260688402/">
<img src="images/console_loggin.jpg">
</a>
</header>
<section class="text" style="margin-top:133px;">
<figure>
<img src="images/console_demo.png">
<figcaption>
Something like you'll see in your console when you click the element. (Firebug)
</figcaption>
</figure>
<figure>
<iframe style="width: 90%; height: 200px; margin:0 auto; display:block;" src="http://jsfiddle.net/ajpiano/VDVzQ/embedded/"></iframe>
<figcaption>
The previous example, in action on <a href="http://jsfiddle.net/ajpiano/VDVzQ/">jsFiddle</a> (more on that later).
</figcaption>
</figure>
</section>
</div>
<div class="slide">
<header>
<h1>The <em>Consolation</em> Prize</h1>
</header>
<section class="smallerCode text">
<p>You don't have to modify your code at all to debug using the Console</p>
<figure>
<img src="images/consolation_prize.png" style="width:100%">
<figcaption>
<ul>
<li>You can execute any arbitrary code just by entering it into the console.</li>
<li><em>Code is executed in the context of the <code>window</code> object</em> (<code>this === window</code>)</li>
<li><em>Unless the debugger is paused, in which case code is executed in the context of the paused function</em></li>
<li><strong>Embiggen your console (if possible)</strong></li>
</ul>
</figcaption>
</figure>
</div>
<div class="slide">
<header>
<h1>A Whole Lotta API</h1>
</header>
<section class="text">
<figure class="floaty-figure">
<img src="images/console_api.png">
<figcaption>
Shamelessly capped from <a href="http://getfirebug.com/wiki/index.php/Console_API">Firebug Wiki</a>
</figcaption>
</figure>
<ul>
<li>There are a lot of methods in the <a href="http://getfirebug.com/wiki/index.php/Console_API">Console API</a></li>
<li>To get started, we only really need <code>log</code>, <code>debug</code>, and <code>dir</code></li>
<li>Unfortunately, most of these suck in IE</li>
<li>Our arsenal is almost complete!</li>
</ul>
</section>
</div>
<div class="slide">
<header>
<h1>He's On Fire!</h1>
</header>
<section class="text">
The <a href="http://firequery.binaryage.com/">FireQuery</a> add-on for Firebug makes debugging jQuery <strong>even easier</strong>
<figure>
<img src="images/firequery_html.png" style="width:40%;">
<img src="images/firequery_console.png" style="width:40%;">
<figcaption>
<ol>
<li>FireQuery showing jQuery.data() properties in HTML View</li>
<li>Pretty-printing jQuery objects in console</li>
</ol>
</figcaption>
</figure>
<ul>
<li>Inject jQuery into any page</li>
<li>Run <a href="http://github.com/jamespadolsey/jQuery-Lint">jQuery Lint</a></li>
<li>If you like this, you might also like <a href="http://firerainbow.binaryage.com/">FireRainbow</a></li>
</ul>
</section>
</div>
<div class="slide">
<header>
<h1>Answer Your Own FAQs</h1>
<h2>AKA: "Why isn't it working?"</h2>
</header>
<section style="margin-top:163px;" class="muchSmallerCode text">
<dl>
<dt><strong>Q: </strong>Why aren't my events happening?</dt>
<dd>Do the elements you're attaching them to even exist? Type this in the console, and run it.</dd>
<dd>
<pre class="brush: js; toolbar: false">
// Replace ".theElements" with your actual selector
console.log($(".theElements").length);
</pre>
</dd>
<dd>If the result is <code>0</code>, <strong>the elements are not in the DOM</strong></dd>
<dd> Checking the <code>length</code> property of jQuery object is the <strong>easiest & fastest</strong> way to check
if a selector matched any elements. <em>Don't use <code>.size()</code>, it's a waste of time.</em></dd>
<dd><strong>A: </strong>A selector you are using is not correct.</dd>
<dd><strong>A: </strong>A traversal failed to match any elements.</dd>
<dt>Don't Quote Me On That</dt>
<dd>If you are referring to DOM Elements like <code>window</code> or <code>this</code>, don't treat them like strings</dd>
<dd>
<pre class="brush: js; toolbar: false">
$('this') // Wrong
$(this) // Right
</pre>
</dd>
<dt><strong>Q: </strong>But the result was <code>> 0</code>!</dt>
<dd><strong>A: </strong>Then we forge on!</dd>
</dl>
</section>
</div>
<div class="slide">
<header>
<h1>"The selection contains elements!"</h1>
</header>
<section class="smallerCode text">
<p>By and large, jQuery works with elements that are in the DOM.</p>
<dl style="line-height:24px">
<dt><strong>Q: </strong>Were the the elements there when the code was actually run?</dt>
<dd>Just because the elements are in the DOM now, doesn't mean they were when the code executed.</dd>
<dt><strong>A1: </strong><code>jQuery(document).ready(function($){});</code></dt>
<dd>Make sure your code was inside of a document-ready block, or otherwise executed after the DOM has loaded.</dd>
<dd>This is just one reason to <a href="http://developer.yahoo.com/blogs/ydn/posts/2007/07/high_performanc_5/">put your scripts at the bottom of the <code>body</code></a>.</dd>
<dt><strong>A2: </strong> Debug!</dt>
<dd>Set a breakpoint at the line of code that is misbehaving. When the code pauses, <code>console.log($("yourSelector").length)</code></dd>
<dd>Add <code>$("yourSelector").length</code> as a Watch Expression, wait for the breakpoint.</dd>
<dd>Place <code>console.log($("yourSelector").length)</code> in your script, right before the code executes.</dd>
</dl>
</section>
</div>
<div class="slide">
<header>
<h1>Log Freely!</h1>
<section>
<p class="quoteThing">
Don't just log <code>.length</code>!<br/>
You can log any type of variable.<br/>
<span style="display:block;font-size:75%;line-height:44px;">
If you log the entire jQuery object,
you can hover over the elements, if any, to see them highlighted in the page!*
</span>
</p>
<p>* IE doesn't support any of this awesome stuff.<br/>In fact, you can't interact with the page at all when you've paused execution in IE.</p>
</section>
</header>
</div>
<div class="slide">
<header>
<h1 style="font-size:44%">I Added The Elements to Page After I Tried To Actually Use Them</h1>
</header>
<section class="text smallerCode">
<ul>
<li>One of the most <a href="http://docs.jquery.com/FAQ#Why_do_my_events_stop_working_after_an_AJAX_request.3F">FA of all Q's</a></li>
<li>Could be as simple as <code>.html()</code> or the result of inserting AJAX-fetched content.</li>
<li>
<pre class="brush: js; toolbar: false">
var foo = $("div.foo").click(function() {
$(this).text("I was clicked!");
});
// foo.length == 1;
// nothing will happen when these divs are clicked.
$("<div>",{"class":"foo"}).appendTo(document.body);
$("div.bar").addClass("foo");
</pre>
</li>
<li>Handlers bound with <code>.bind()</code> (and shortcuts) are only <strong>attached</strong> to elements that
are in the jQuery object when <code>.bind()</code> is executed.</li>
</ul>
</section>
</div>
<div class="slide">
<header>
<h1>Hence, <code>.live()</code> and <code>.delegate</code></h1>
</header>
<section class="text muchSmallerCode">
<ul>
<li><strong>Event delegation</strong> uses <a href="http://www.quirksmode.org/js/events_order.html">event bubbling</a> to catch events at a higher level in the DOM than
where they occurred.</li>
<li>Now, you can catch events that happen on newly added elements</li>
</ul>
<pre class="brush: js; toolbar: false">
// All three examples will catch a click on any anchor that is ever in the page
$(document).bind("click",function(e) {
if ($(e.target).is("a") {
// this === document
}
});
$(document).delegate("a","click",function(e) {
//this === clicked anchor
});
$("a").live("click",function(e) {
//this === clicked anchor
});
</pre>
</section>
</div>
<div class="slide">
<header>
<h1><code>bind()</code> is not <code>live()</code></h1>
</header>
<section class="text muchSmallerCode">
<p>They do similar things. How are they different?</p>
<pre class="brush: html; toolbar: false;">
<div id="teddy" class="foo">
<div id="roosevelt" class="bar">
</pre>
<pre class="brush: js; toolbar: false;">
var log = function() { console.log(this.id); }
$(".foo").click(log);
$(".bar").live("click",log);
$(".foo").addClass("bar").removeClass("foo").trigger("click");
// logs 'teddy' twice
// 1) Because there is still a click handler attached to <div id='teddy'></div>
// even though it no longer has the class 'foo'
// 2) Because a "click" on an element with class "bar" bubbled to the document
</pre>
<ul>
<li><code>.bind()</code> creates a 1:1 association between element and handler</li>
<li><code>.live()</code> and <code>.delegate()</code> check to see if the target element of a bubbled event matches certain criteria</li>
</ul>
</section>
</div>
<div class="slide">
<header>
<h1>jQuery Objects are <em>not</em> live</code></h1>
</header>
<section class="text smallerCode">
<p>New elements that match a selector do not automagically appear in existing collections</p>
<pre class="brush: html; toolbar: false;">
<ul id="wheatBeers" class="delicious">
<li>Abita Purple Haze</li>
<li>Hoegaarden</li>
<li>UFO Hefeweizen</li>
</ul>
</pre>
<pre class="brush: js; toolbar: false;">
var li = $("#wheatBeers li");
// li.length == 3;
$("<li>",{text:"Weihenstephaner"})
.appendTo("#wheatBeers");
// guess what? li.length == 3
</pre>
</section>
</div>
<div class="slide">
<header>
<h1>jQuery objects are <a href="http://www.dictionary.net/transient">transient</a></h1>
</header>
<section class="text muchSmallerCode">
<ul style="line-height:18px;font-size:80%">
<li>Every call to jQuery's traversal/selection methods returns a <strong>new</strong> jQuery object</li>
<li>That's why you <strong>can't compare jQuery objects for equality</strong></li>
<li>
<pre class="brush: js; toolbar: false;">
$("#foo") === $("#foo"); // false
$("#foo")[0] === $("#foo")[0] // true
</pre>
</li>
<li><strong>DON'T</strong> store arbitrary data on random jQuery objects! <em>Hint: Use .data()</em></li>
<li>
<pre class="brush: js; toolbar: false;">
var $myCoolWidget = $("#cycleMcParallax");
$myCoolWidget.cycleCount = 0;
</pre>
</li>
<li><strong>Beware</strong> of <code>add()</code></li>
<li>
<pre class="brush: js; toolbar: false;">
var red = $(".red"), green = $(".green");
// red.length == 5; green.length == 5;
green.add(red);
// green.length == 5;
green = green.add(red);
// green.length == 10;
</pre>
</li>
</ul>
</section>
</div>
<div class="slide">
<header>
<h1>What is <code>this</code>? I don't even...</h1>
</header>
<section class="text smallerCode">
<ul style="line-height:22px">
<li>You pass a lot of functions to various jQuery methods</li>
<li>Event Handlers, AJAX callbacks, Utility methods, Plugin callbacks</li>
<li>jQuery and plugins <strong>will</strong> change the 'this' (the scope) of these functions</li>
<li><em>Just being aware of this fact is half the battle</em></li>
</ul>
<figure>
<pre class="brush: js; toolbar: false">
$("#foo").click(function(e) {
$.getJSON("/ajax/page/",function(data) {
$(this).text(data.status); // IT'S NOT WORK
});
});
</pre>
<figcaption>
This <code>this</code> example gives new jQuery users fits.
</figcaption>
</figure>
<ul style="line-height:22px">
<li><strong>Be <em>very</em> aware of this when writing object-oriented code</strong></li>
</ul>
</section>
</div>
<div class="slide">
<header>
<h1><code>this</code> is out of control!</h1>
</header>
<section class="text muchSmallerCode">
<dl>
<dt>Event handlers, <code>jQuery.fn.each</code>, jQuery UI</dt>
<dd>Explicitly set <code>this</code> to be a DOM element</dd>
<dt>AJAX callbacks</dt>
<dd>Explicitly set <code>this</code> to be the options object</dd>
<dt><code>jQuery.each</code></dt>
<dd>Explicitly sets <code>this</code> to be the value being iterated, but weird.</dd>
</dl>
<figure>
<pre class="brush: js; toolbar: false">
$.each(["foo","bar"],function(i,value) {
// this == ["f","o","o"]
// value == "foo"
});
</pre>
<figcaption>
<em>jQuery.each: Don't use <code>this</code> here, use the second argument</em>
</figcaption>
</figure>
<dl>
<dt>Random Plugins With Callbacks</dt>
<dd>Might set <code>this</code>, might not. Be on the lookout.</dd>
</dl>
</section>
</div>
<div class="slide">
<header>
<h1>Don't drop the scope</h1>
</header>
<section class="text">
<p>
If <strong><code>this</code> isn't the <code>this</code> you think <code>this</code> is</strong>,
you can use the debugger and/or the console to confirm your suspicion.
</p>
<figure>
<img src="images/what_is_this.png" style="width:100%">
<figcaption>
Hmm, <code>this</code> doesn't look like a DOM element from here!
</figcaption>
</figure>
</section>
</div>
<div class="slide">
<header>
<h1>Let's get <code>this</code> straight</h1>
</header>
<section class="text muchSmallerCode">
<dl>
<dt>Use a variable to alias <code>this</code></dd>
<dd>
<pre class="brush: js; toolbar: false; highlight:[2,4]">
$("#foo").click(function(e) {
var elem = this;
$.getJSON("/ajax/page/",function(data) {
$(elem).text(data.status); // ITS WORK NOW!
});
});
</pre>
</dd>
<dt><code><a href="http://api.jquery.com/jQuery.proxy">jQuery.proxy</a></code></dt>
<dd>Returns a reference to a function that will run with a particular context</dd>
<dd>
<pre class="brush: js; toolbar: false; highlight:[4,8]">
var person = {
name:"Ace S. McCloud",
init:function() {
$("#sayHi").click($.proxy(function(e) {
// Now 'this' refers to the person object!
// We can still use e.target to reference the element
$(e.target).text(this.name);
},this));
}
};
</pre>
</dd>
</dl>
</section>
</div>
<div class="slide">
<header>
<h1><code>this</code> shall not pass!</h1>
<h2><em>Referencing</em> vs. <em>executing</em> a function</h2>
</header>
<section class="text muchMuchSmallerCode" style="margin-top:173px">
<pre class="brush: js; toolbar: false;">
// A simple function
function log(str) {
console.log(str);
};
// Logs an event object, because jQuery always passes the event object as the first argument to handlers
$("#foo").click(log);
</pre>
<p>You <strong>cannot</strong> override supplied function arguments by "passing arguments" to a named function:
<em>You'll end up executing the function!</em></p>
<pre class="brush: js; toolbar: false;">
// WRONG
$("#foo").click(log("I clicked it"));
// You just executed 'log', and failed to pass a function as a handler. Huh?
// It's the same as doing the following, and just as broken.
var l = log("I clicked it"); // l === undefined
$("#foo").click(l);
// RIGHT
$("#foo").click(function(e) {
log("I clicked it");
});
</pre>
</section>
</div>
<div class="slide interstitial">
<header>
<h1>Debugging AJAX</h1>
</header>
<section class="text">
<figure>
<img src="images/ajax_injury.jpg">
<figcaption>
<a href="http://www.flickr.com/photos/ugocei/103381013/sizes/z/in/photostream/">http://www.flickr.com/photos/ugocei/103381013/sizes/z/in/photostream/</a>
</figcaption>
</figure>
</section>
</div>
<div class="slide">
<header>
<h1>Question #1</h1>
<section>
<p class="quoteThing">
Was there an actual request?
</p>
</section>
</header>
</div>
<div class="slide debugger-screenshots">
<header>
<h1>The Network/Resources Tab</h1>
</header>
<section class="text">
<p>Investigate the existence and results of AJAX calls</p>
<figure>
<img src="images/firebug_ajax_console.png">
<img src="images/firebug_net_xhr.png" style="margin-top:10px;">
<figcaption>
Firebug shows XHR activity in the Console and the Net tabs.
</figcaption>
</figure>
<figure>
<img src="images/chrome_net_xhr.png">
<figcaption>
Newest versions of WebKit Inspector use the Network tab.<br/>
Current releases use the Resources tab
</figcaption>
</figure>
</section>
</div>
<div class="slide debugger-screenshots">
<header>
<h1 style="font-size:80%">Did the request even happen? IE edition</h1>
</header>
<section class="text">
<p>IE's debugging tools do not allow you to interrogate XHRs<br/>
You have to use a third-party HTTP Proxy tool like
<a href="http://www.charlesproxy.com/">Charles</a> or <a href="http://www.fiddler2.com/fiddler2/version.asp">Fiddler</a>
</p>
<figure>
<img src="images/ie_charles.png" style="width:75%;margin:0 auto">
<figcaption>
Charles, shown here, pairs with IE (and other browsers) to allow you to explore XHR calls,
similar to the Net tab in other debuggers
</figcaption>
</figure>
</section>
</div>
<div class="slide">
<header>
<h1>If You Don't See an XHR</h1>
</header>
<section class="text muchSmallerCode">
<dl>
<dt>Likely Explanation</dt>
<dd>The request was never made. Something else in your code broke/misbehaved before the AJAX call was even fired.</dd>
<dd><strong>Protip: </strong> Add a <code>beforeSend</code> callback to check if the <code>$.ajax()</code> function is being reached.</dd>
<dd>
<pre class="brush: js; toolbar: false; highlight:[5,6,7]">
$.ajax({
url:"foo.php",
data:{hello:"there"},
success:function(data) {},
beforeSend:function(xhr) {
console.debug("The XHR object has been prepared ",xhr);
}
});
</pre>
</dd>
<dt>Considerably Less Likely Explanation</dt>
<dd>The request does not use an <code>XMLHttpRequest</code>, and is not logged by these tools.</dd>
<dd>Certain AJAX-y interactions don't actually use an XHR to communicate.
<ul>
<li><strong>JSONP: </strong> Creates a new <code>script</code> node and appends it to the <code>head</code>.</li>
<li><strong>'AJAX' Upload: </strong>Uses an <code>iframe</code> or Flash as a proxy to do the upload.</li>
</ul>
</dd>
</dl>
</section>
</div>
<div class="slide">
<header>
<h1>Haven't Had Much <code>success</code></h1>
</header>
<section class="text muchSmallerCode">
<p>If you are able to confirm that an XHR has indeed taken place, but your success callback isn't firing:</p>
<dl>
<dt><strong>A1: </strong>Server Error</dt>
<dd>The resource you are requesting is returning some sort of error. Use the Network tab of your
debugger to see the StatusCode and physical response from the server.</dd>
<dt><strong>A2: </strong>Invalid JSON</dt>
<dd>If you are using <code>jQuery.getJSON</code> or otherwise specifying a <code>dataType</code>
of JSON, jQuery will not fire the success callback if it cannot parse the response as <em>valid</em> JSON.</dd>
<dd><a href="http://simonwillison.net/2006/Oct/11/json/">Valid JSON</a> has <strong>double quotes</strong> around all strings, including keys, and is typically generated by a JSON encoder.</dd>
<dt><strong>A3: </strong>Malformed HTML</dt>
<dd>If you are using <code>jQuery.fn.load</code> or otherwise appending HTML that you fetched via AJAX,
make sure the HTML is structurally valid and does not contain errors.</dd>
</dl>
</section>
</div>
<div class="slide">
<header>
<h1>Not <code>this</code> again!</h1>
</header>
<section class="text muchMuchSmallerCode">
<p style="margin:.3em 0;">If your success callback is firing, but not working right</p>