/
atom.xml
3603 lines (2768 loc) · 289 KB
/
atom.xml
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
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[TJ VanToll]]></title>
<link href="http://tjvantoll.com/atom.xml" rel="self"/>
<link href="http://tjvantoll.com/"/>
<updated>2012-08-20T21:39:01-04:00</updated>
<id>http://tjvantoll.com/</id>
<author>
<name><![CDATA[TJ VanToll]]></name>
<email><![CDATA[tj.vantoll@gmail.com]]></email>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[onscroll Event Issues on Mobile Browsers]]></title>
<link href="http://tjvantoll.com/2012/08/19/onscroll-event-issues-on-mobile-browsers/"/>
<updated>2012-08-19T00:00:00-04:00</updated>
<id>http://tjvantoll.com/2012/08/19/onscroll-event-issues-on-mobile-browsers</id>
<content type="html"><![CDATA[<p>All browsers fire an <code>onscroll</code> event on the <code>window</code> object whenever the window is scrolled. On desktop browsers this event is fired continuously as the user scrolls, but on most all mobile browsers the event is not fired until the <em>scrolling action</em> comes to a complete stop.</p>
<!--more-->
<p>You can see this by scrolling in the example below:</p>
<iframe style="width: 100%; height: 300px;" src="http://jsfiddle.net/tj_vantoll/p4pww/13/embedded/result,html,js,css/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<p>The <code>onscroll</code> event count and the value of <code>window.scrollY</code> (<a href="https://developer.mozilla.org/en-US/docs/DOM/window.scrollY">the number of pixels the document has been scrolled vertically</a>) displayed on the top of the screen in the example are updated in an <code>onscroll</code> event handler.</p>
<p>If you’re viewing this on any desktop browser you’ll see that as you scroll the <code>onscroll</code> event is continuously firing, <code>window.scrollY</code> is continuously updating, and the blue box (which is present so you can visibly tell whether the browser re-paints the screen) is continuously moving.</p>
<h3>Enter Mobile</h3>
<p>If you try the same demo on iOS Safari (5.0), the default Android browser <= 2.3, Opera Mobile, or IE on Windows Phone 7 you’ll notice something quite different. As you scroll the <code>onscroll</code> event isn’t fired, <code>window.scrollY</code> isn’t updated, and the blue box does not move until the scrolling has come to a complete stop.</p>
<p>You can see this in the video below (the video shows iOS Safari but the same behavior occurs in the other listed browsers):</p>
<iframe width="420" height="315" src="http://www.youtube.com/embed/5-vOJEP3x28" frameborder="0" allowfullscreen></iframe>
<h3>Why</h3>
<p>These mobile browsers simply do not fire the <code>onscroll</code> event until scrolling has completely stopped. This includes not only the touch based scrolling itself, but additionally any momentum that the user gives on the scroll. The event will not fire until it stops. This is a problem if you want to apply a visual change to the screen as the user scrolls.</p>
<h3>Other Mobile Browsers</h3>
<p>Firefox for Android does fire the <code>onscroll</code> event and updates <code>window.clientY</code> as you scroll, but strangely it doesn’t re-paint the screen for any changes that have been applied.</p>
<p>The Android browser in Ice Cream Sandwich fires the event but doesn’t feel very responsive and only sporadically re-paints the DOM to move the blue box. Luckily, Jelly Bean’s Android browser handles this example perfectly; everything is updated and rendered smoothly as the user scrolls.</p>
<h3>The Problem</h3>
<p>In my case I wanted to apply a change to the DOM for every pixel that the user scrolled, exactly like moving the blue box in the example above.</p>
<p>So the question is, can we work around this limitation and get desktop <code>onscroll</code> functionality in a mobile friendly way?</p>
<h3>Workaround Attempt - setInterval</h3>
<p>My first attempt was to set an interval that did what I wanted to do in the <code>onscroll</code> event. Yes the code will run continuously instead of just when the user scrolls, but it’s somewhere to start.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">setInterval</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="c1">// Logic</span>
</span><span class='line'><span class="p">},</span> <span class="mi">20</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>
<p>The problem with this approach is that iOS Safari, Android <= 2.3, and Opera Mobile do not run any functions queued through <code>setInterval</code> or <code>setTimeout</code> while a scroll is being performed. The execution will simply be paused until the scroll has completed.</p>
<p>Here’s an example that simply appends an asterisk to a div every 500 milliseconds using <code>setInterval</code>:</p>
<iframe style="width: 100%; height: 300px;" src="http://jsfiddle.net/tj_vantoll/NfkEg/7/embedded/result,js,html,css/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<p>If you’re viewing this in a desktop browser and you scroll, you can see that the asterisks will continue to be created.</p>
<p>However, on the affected mobile browsers (iOS Safari, Android <= 2.3, Opera Mobile), because the function queued through <code>setInterval</code> is paused, asterisk creation stops the moment you start scrolling and doesn’t resume until you stop.</p>
<p>This video shows this behavior on iOS Safari (5.0):</p>
<iframe width="560" height="315" src="http://www.youtube.com/embed/XkLvV9aPcYQ" frameborder="0" allowfullscreen></iframe>
<p>This example works perfectly (scrolling doesn’t stop asterisk creation) on the default Ice Cream Sandwich / Jelly Bean browser, Firefox for Android, and IE for Windows Phone 7.</p>
<h3>Workaround Attempt 2 - Use Touch Events</h3>
<p>Since the <code>setInterval</code> approach failed on the big mobile browsers my next thought was to use touch events instead.</p>
<p>Most mobile browsers fire <a href="http://blog.jquery.com/2012/04/10/getting-touchy-about-patents/">Apple’s flavor</a> of <a href="https://developer.mozilla.org/en-US/docs/DOM/Touch_events">touch events</a> as the user interacts with the screen via touch (the notable exception being Window’s Mobile since Microsoft has their own touch model).</p>
<p>In particular the <code>ontouchmove</code> event is fired as the user moves their finger (or stylus, etc) across the screen. Since users on touch devices need to move their finger across the screen to scroll, this seemed like the perfect alternative to <code>onscroll</code>.</p>
<p>Therefore I modified my example to use <code>ontouchmove</code> instead of <code>onscroll</code>:</p>
<iframe style="width: 100%; height: 300px;" src="http://jsfiddle.net/tj_vantoll/RFdve/10/embedded/result,js,html,css/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<p>If you scroll on the above example on a desktop browser nothing will be updated since the counters are being driven by the <code>ontouchmove</code> event. On mobile browsers a wide variety of things happen:</p>
<ul>
<li>Android: The <code>ontouchmove</code> event does get fired as the user moves the screen. However the DOM updates are very sporadic and feel very jerky. This is true of the default Android browser in Gingerbread, Ice Cream Sandwich, and Jelly Bean although it gets better in later versions.</li>
<li>Firefox for Android: The <code>ontouchmove</code> events fires but DOM updates made in the <code>ontouchmove</code> event take effect sporadicly if at all. Everything feels very jerky at best.</li>
<li>Opera Mobile: <code>ontouchmove</code> events occur but DOM changes are not applied until scrolling is complete.</li>
<li>iOS Safari: On <code>ontouchmove</code> event is fired as the screen is moved and the DOM does get re-painted. This is only mobile browser where this approach made a substantial difference.</li>
</ul>
<p>One consistent issue with this approach is that the <code>ontouchmove</code> event is only fired when the user’s finger remains on the screen. Meaning, if the user gives any momentum to the scroll, <code>ontouchmove</code> events will not be fired while the window is scrolling and their finger is not on the screen.</p>
<p>You can see this in the video below:</p>
<iframe width="420" height="315" src="http://www.youtube.com/embed/wied94KmwKw" frameborder="0" allowfullscreen></iframe>
<p>So what does all of this mean about using the <code>ontouchmove</code> event to mimic desktop <code>onscroll</code> functionality? At the moment there are too many inconsistencies to rely on this behavior in any way. If you only need to support iOS Safari this approach works reasonably.</p>
<h3>Workaround Attempt 3 - Don’t <em>Really</em> Scroll</h3>
<p>Another <em>solution</em> out there is to disable native scrolling altogether and use JavaScript to mimic scrolling instead.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">$</span><span class="p">(</span><span class="s1">'window'</span><span class="p">).</span><span class="nx">on</span><span class="p">(</span><span class="s1">'touchmove'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="c1">//Prevent the window from being scrolled.</span>
</span><span class='line'> <span class="nx">event</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">//Do something like call window.scrollTo to mimic the scrolling</span>
</span><span class='line'> <span class="c1">//request the user made.</span>
</span><span class='line'><span class="p">});</span>
</span></code></pre></td></tr></table></div></figure>
<p>Unfortunately such techniques are usually utilized to create fixed height/width scrolling areas and are not intended (nor especially practical) for full screens. If you are only interested in a scrolling event for a small section of the page you might want to look into something such as <a href="http://cubiq.org/iscroll-4">iScroll 4</a>.</p>
<h3>Conclusion</h3>
<p>Unlike desktop browsers, most all mobile browsers simply do not fire an <code>onscroll</code> event until the scrolling action comes to a complete stop.</p>
<p>The only mobile browser that handled this event elegantly in my testing was Android’s Jelly Bean browser. Therefore, if you need any sort of cross browser support you’re simply out of luck; there is simply no cross browser viable workaround to mimic the desktop behavior. If you have had success implementing this by some other means please let me know in the comments.</p>
<h3>Disclaimer</h3>
<p>I haven’t been able to test this in Chrome for Android and I know there are other mobile browsers that I’m missing. If someone else has this capability I’d love to know how they handle these situations.</p>
<p>Also while I did verify these findings on physical devices for Firefox for Android, Android 2.3’s default browser, and Safari on iOS 5; the rest of my testing was limited to simulators / emulators. From past experience I know that simulator / emulator testing is no substitute for the real thing. Therefore, if you find any discrepancies in my findings please let me know in the comments so I can update the post.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Subpixel Animation Issues in IE < 9]]></title>
<link href="http://tjvantoll.com/2012/08/16/subpixel-animation-issues-in-ie-less-than-9/"/>
<updated>2012-08-16T21:56:00-04:00</updated>
<id>http://tjvantoll.com/2012/08/16/subpixel-animation-issues-in-ie-less-than-9</id>
<content type="html"><![CDATA[<p>While there are definitely <a href="http://ejohn.org/blog/sub-pixel-problems-in-css/">cross browser discrepancies handling subpixels</a>, this one caught me by surprise. Take the following:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><div</span> <span class="na">id=</span><span class="s">"box"</span><span class="nt">></div></span>
</span><span class='line'><span class="nt"><script></span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">box</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'box'</span><span class="p">);</span>
</span><span class='line'> <span class="nx">box</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="s1">'10.25px'</span><span class="p">;</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">box</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">);</span>
</span><span class='line'><span class="nt"></script></span>
</span></code></pre></td></tr></table></div></figure>
<p>This simply sets a value for the <code>left</code> CSS property then immediately retrieves it. In Chrome 22, Firefox 14, Safari 6, Opera 12, IE >= 9, iOS 5, and Android <code>10.25px</code> will be logged.</p>
<p>In IE < 9 <code>10px</code> is logged. While not all browsers can accurately render the subpixel values, I had assumed all of them would’ve at least allowed the assignment. I was wrong. Internet Explorer will simply round the value to the nearest integer.</p>
<p>Why is this a problem? <!--more-->Take the following code.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><div</span> <span class="na">id=</span><span class="s">"box"</span><span class="nt">></div></span>
</span><span class='line'><span class="nt"><script></span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">box</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'box'</span><span class="p">);</span>
</span><span class='line'> <span class="nx">box</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="s1">'0px'</span><span class="p">;</span>
</span><span class='line'> <span class="nx">setInterval</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">currentLeft</span> <span class="o">=</span> <span class="nx">box</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="s1">'px'</span><span class="p">,</span> <span class="s1">''</span><span class="p">);</span>
</span><span class='line'> <span class="nx">currentLeft</span> <span class="o">=</span> <span class="nb">parseFloat</span><span class="p">(</span><span class="nx">currentLeft</span><span class="p">);</span>
</span><span class='line'> <span class="nx">box</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nx">currentLeft</span> <span class="o">+</span> <span class="mf">0.25</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
</span><span class='line'> <span class="p">},</span> <span class="mi">20</span><span class="p">);</span>
</span><span class='line'><span class="nt"></script></span>
</span></code></pre></td></tr></table></div></figure>
<p>This sets an interval that will increase the <code>left</code> property of a box by <code>0.25</code> pixels every 20 milliseconds. Doing so will move the box left across the screen as seen below:</p>
<iframe style="width: 100%; height: 150px;" src="http://jsfiddle.net/tj_vantoll/PBsLt/9/embedded/result,js,html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<p>Great. Unfortunately in IE < 9 the box will not move. Within the interval function the value to increment is being retrieved from the element itself, which, in IE < 9 will continuously return the rounded value.</p>
<p>The way around this is simply to use store off the value of the property outside of the animation loop itself.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><div</span> <span class="na">id=</span><span class="s">"box"</span><span class="nt">></div></span>
</span><span class='line'><span class="nt"><script></span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">left</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">box</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'box'</span><span class="p">);</span>
</span><span class='line'> <span class="nx">setInterval</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">box</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">left</span> <span class="o">=</span> <span class="nx">left</span> <span class="o">+</span> <span class="s1">'px'</span><span class="p">;</span>
</span><span class='line'> <span class="nx">left</span> <span class="o">+=</span> <span class="mf">0.25</span><span class="p">;</span>
</span><span class='line'> <span class="p">},</span> <span class="mi">20</span><span class="p">);</span>
</span><span class='line'><span class="nt"></script></span>
</span></code></pre></td></tr></table></div></figure>
<p>This has the added benefit of being more efficient since you save a property retrieval on every invocation of the loop.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[LESS 101 - The Basics of the CSS Preprocessor]]></title>
<link href="http://tjvantoll.com/2012/08/10/less-101-the-basics-of-the-css-preprocessor/"/>
<updated>2012-08-10T00:00:00-04:00</updated>
<id>http://tjvantoll.com/2012/08/10/less-101-the-basics-of-the-css-preprocessor</id>
<content type="html"><![CDATA[<p>I gave a “LESS 101” talk to my coworkers at Liquid Web today. I thought I’d post the slides here in case anyone else might find them useful. They are <a href="http://tjvantoll.com/speaking/slides/2012/LESS-101">here</a>.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[HTML5 Form Validation - Showing All Error Messages]]></title>
<link href="http://tjvantoll.com/2012/08/05/html5-form-validation-showing-all-error-messages/"/>
<updated>2012-08-05T16:21:00-04:00</updated>
<id>http://tjvantoll.com/2012/08/05/html5-form-validation-showing-all-error-messages</id>
<content type="html"><![CDATA[<p><a href="http://caniuse.com/#feat=form-validation">Browsers that support HTML5 form validation</a> have one thing in common; if a <code><form></code> is submitted and has errors on multiple fields, the browser will only display the first error to the user.</p>
<p>Turns out the spec leaves the specific means of handling multiple errors up to the browser itself:</p>
<blockquote><p>Report the problems with the constraints of at least one of the elements given in unhandled invalid controls to the user. User agents may focus one of those elements in the process, by running the focusing steps for that element, and may change the scrolling position of the document, or perform some other action that brings the element to the user’s attention.</p><p>User agents may report more than one constraint violation. User agents may coalesce related constraint violation reports if appropriate (e.g. if multiple radio buttons in a group are marked as required, only one error need be reported).</p><footer><strong>HTML5 Specification</strong> <cite><a href='http://www.whatwg.org/specs/web-apps/current-work/#the-constraint-validation-api'>www.whatwg.org/specs/web-apps/…</a></cite></footer></blockquote>
<p>The key part here being that user agents (i.e. browsers) <strong>MAY</strong> report more than one constraint violation (i.e. error). Turns out they all decided not to.</p>
<!--more-->
<p>You can see this in your browser below (assuming it <a href="http://caniuse.com/#feat=form-validation">supports HTML5 form validation</a> and is not Safari, more on that later). Both fields are <code>required</code>, but if you submit the form you will only see an error for the first field.</p>
<pre class="codepen" data-type="result" data-href="FBGvu" data-user="tjvantoll" data-host="http://codepen.io"><code></code></pre>
<script async src="http://codepen.io/assets/embed/ei.js"></script>
<p>Here’s what it looks like on supported browsers if you attempt to submit this empty <code><form></code>:</p>
<h5>Chrome 21</h5>
<p><img src="http://tjvantoll.com/images/posts/2012-08-05/Chrome.png" title="Chrome" alt="Chrome" /></p>
<h5>Firefox 14</h5>
<p><img src="http://tjvantoll.com/images/posts/2012-08-05/Firefox.png" title="Firefox" alt="Firefox" /></p>
<h5>Opera 12</h5>
<p><img src="http://tjvantoll.com/images/posts/2012-08-05/Opera.png" title="Opera" alt="Opera" /></p>
<p>As you can see, all three only give an error for the first field. Firefox at least has the decency to put a red border around all fields with invalid data by default.</p>
<p>The one noticeable browser missing from the list above is Safari. Even though Safari supports the constraint validation API, the validation itself is turned off.</p>
<h3>Usability</h3>
<p>From a usability perspective showing the users only the first error message is bad. Imagine how frustrating it would be to continually correct errors just to be presented with the next error in the sequence. If you’ve ran into a form such as this before you know what I’m talking about.</p>
<p>Luckily, browsers provide a <a href="http://www.whatwg.org/specs/web-apps/current-work/#the-constraint-validation-api">constraint validation API</a> that can be used to provide this functionality.</p>
<h3>Using the Validation API</h3>
<p>All dom nodes now possess a <a href="http://www.whatwg.org/specs/web-apps/current-work/#dom-cva-willvalidate_">willValidate</a> property that indicates whether the node is a candidate for form validation.</p>
<p>Nodes in which <code>willValidate</code> is <code>true</code> also have a <code>validity</code> property. The <code>validity</code> property resolves to a <a href="https://developer.mozilla.org/en-US/docs/DOM/ValidityState">ValidityState object</a> which contains information about whether the field has validation errors, as well as the error message the browser will display to the user. You can leverage this API to display all error messages whenever a <code><form></code> is submitted.</p>
<h3>The Code</h3>
<p>Here’s how I accomplished this with a jQuery dependent script.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><form></span>
</span><span class='line'> <span class="nt"><ul</span> <span class="na">id=</span><span class="s">"errorMessages"</span><span class="nt">></ul></span>
</span><span class='line'>
</span><span class='line'> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"name"</span><span class="nt">></span>Name:<span class="nt"></label></span>
</span><span class='line'> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">required</span> <span class="nt">/></span>
</span><span class='line'>
</span><span class='line'> <span class="nt"><label</span> <span class="na">for=</span><span class="s">"comments"</span><span class="nt">></span>Comments:<span class="nt"></label></span>
</span><span class='line'> <span class="nt"><textarea</span> <span class="na">id=</span><span class="s">"comments"</span> <span class="na">required</span><span class="nt">></textarea></span>
</span><span class='line'>
</span><span class='line'> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"submit"</span> <span class="na">value=</span><span class="s">"Submit"</span> <span class="nt">/></span>
</span><span class='line'><span class="nt"></form></span>
</span><span class='line'>
</span><span class='line'><span class="nt"><script></span>
</span><span class='line'><span class="nx">$</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="c1">//Keep track of whether there are any errors on the form for Safari.</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">formHasErrors</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">var</span> <span class="nx">showAllErrorMessages</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'#errorMessages'</span><span class="p">).</span><span class="nx">empty</span><span class="p">();</span>
</span><span class='line'> <span class="nx">formHasErrors</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">//Find everything within the form</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'form'</span><span class="p">).</span><span class="nx">find</span><span class="p">(</span><span class="s1">'*'</span><span class="p">).</span><span class="nx">each</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="nx">node</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">node</span><span class="p">.</span><span class="nx">willValidate</span> <span class="o">&&</span> <span class="nx">node</span><span class="p">.</span><span class="nx">validity</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">node</span><span class="p">.</span><span class="nx">validity</span><span class="p">.</span><span class="nx">valid</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">formHasErrors</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
</span><span class='line'> <span class="c1">//Find the field's corresponding label</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">label</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="s1">'label[for='</span> <span class="o">+</span> <span class="nx">node</span><span class="p">.</span><span class="nx">id</span> <span class="o">+</span> <span class="s1">']'</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">//Opera incorrectly does not fill the validationMessage property.</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">message</span> <span class="o">=</span> <span class="nx">node</span><span class="p">.</span><span class="nx">validationMessage</span> <span class="o">||</span> <span class="s1">'Invalid value.'</span><span class="p">;</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'#errorMessages'</span><span class="p">)</span>
</span><span class='line'> <span class="p">.</span><span class="nx">show</span><span class="p">()</span>
</span><span class='line'> <span class="p">.</span><span class="nx">append</span><span class="p">(</span><span class="s1">'<li><span>'</span> <span class="o">+</span> <span class="nx">label</span><span class="p">.</span><span class="nx">html</span><span class="p">()</span> <span class="o">+</span> <span class="s1">'</span> '</span> <span class="o">+</span> <span class="nx">message</span> <span class="o">+</span> <span class="s1">'</li>'</span><span class="p">);</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'input[type=submit]'</span><span class="p">).</span><span class="nx">on</span><span class="p">(</span><span class="s1">'click'</span><span class="p">,</span> <span class="nx">showAllErrorMessages</span><span class="p">);</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'input[type=text]'</span><span class="p">).</span><span class="nx">on</span><span class="p">(</span><span class="s1">'keypress'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="c1">//keyCode 13 is Enter</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">event</span><span class="p">.</span><span class="nx">keyCode</span> <span class="o">==</span> <span class="mi">13</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">showAllErrorMessages</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'>
</span><span class='line'> <span class="c1">//Handle for Safari not having HTML5 form validation active.</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'form'</span><span class="p">).</span><span class="nx">on</span><span class="p">(</span><span class="s1">'submit'</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">event</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="k">if</span> <span class="p">(</span><span class="nx">formHasErrors</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">event</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'><span class="p">});</span><span class="err"></span>
</span><span class='line'><span class="nt"></script></span>
</span></code></pre></td></tr></table></div></figure>
<p>You can see the results in your browser below:</p>
<pre class="codepen" data-type="result" data-href="eLvlf" data-user="tjvantoll" data-host="http://codepen.io"><code></code></pre>
<p>Here’s how it looks in Chrome 21:</p>
<p><img src="http://tjvantoll.com/images/posts/2012-08-05/Chrome-full.png" title="Chrome" alt="Chrome" /></p>
<p>A couple things to note:</p>
<p>1) If a user attempts to submit a form and gets validation errors, a <code>submit</code> event is never fired for the <code><form></code>. Therefore, instead of listening for <code>submit</code> on the <code><form></code>, I instead listen for a <code>click</code> on the <code><input type="submit"></code>. Since the user is also able to submit the form pressing enter in text inputs, I attach a <code>keypress</code> listener to them to ensure the same logic runs.</p>
<p>2) In my example I start each error message with the contents of the field’s <code><label></code>. This is because the messages for each field are often identical. An alternative approach would be to use another constraint validation API method, <a href="http://www.whatwg.org/specs/web-apps/current-work/#dom-cva-setcustomvalidity">setCustomValidity</a> to set a completely custom message.</p>
<p>3) The <code>node.willValidate && node.validity</code> check will be <code>false</code> in all browsers that do not support the constraint validation API. Therefore this code will simply do nothing in browsers that do not support HTML5 form validation.</p>
<p>4) Opera incorrectly does not fill the <code>validationMessage</code> property. Therefore the check <code>var message = node.validationMessage || 'Invalid value.'</code> is necessary so a message is displayed for Opera.</p>
<p>5) In order to make Safari display the error messages I manually keep track of whether there are any validation errors in the <code><form></code>. If there are I prevent the <code><form></code> from submitting in a <code>submit</code> event.</p>
<p>6) I do nothing to style the individual fields based on whether they have valid data. The HTML5 spec provides a number of CSS hooks to do this and I would recommend reading <a href="http://html5doctor.com/css3-pseudo-classes-and-html5-forms/">CSS Pseudo-Classes and HTML5 Forms</a> from <a href="http://html5doctor.com">html5 Doctor</a> if you’re interested in including such styling.</p>
<h3>That’s a Lot of Code to Do Something Simple</h3>
<p>Yep. While browser support is getting to be quite good for HTML5 forms the implementations themselves are still a bit buggy. Nevertheless, this approach will work for displaying all validation errors to the end user.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[DOM Element References as Global Variables]]></title>
<link href="http://tjvantoll.com/2012/07/19/dom-element-references-as-global-variables/"/>
<updated>2012-07-19T22:33:00-04:00</updated>
<id>http://tjvantoll.com/2012/07/19/dom-element-references-as-global-variables</id>
<content type="html"><![CDATA[<p>Quiz: What is logged when the following markup is rendered?</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><html></span>
</span><span class='line'> <span class="nt"><head></head></span>
</span><span class='line'> <span class="nt"><body></span>
</span><span class='line'> <span class="nt"><button</span> <span class="na">id=</span><span class="s">"bar"</span><span class="nt">></span>Button<span class="nt"></button></span>
</span><span class='line'> <span class="nt"><script></span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">bar</span><span class="p">);</span>
</span><span class='line'> <span class="nt"></script></span>
</span><span class='line'> <span class="nt"></body></span>
</span><span class='line'><span class="nt"></html></span>
</span></code></pre></td></tr></table></div></figure>
<p>Syntax error obviously, right? Wrong. All major browser rendering engines will log a reference to the <code><button></code> node. This includes Trident (IE), Gecko (Firefox), WebKit (Chrome, Safari, etc), and Presto (Opera).</p>
<h3>Wait. What?</h3>
<p>Ah, I get it, there’s no doctype on that markup. So this a quirks mode only thing then right? Wrong. <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=622491">As of Firefox 14</a> the latest version of all major browsers will produce the same result IN STANDARDS MODE.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="cp"><!DOCTYPE html></span>
</span><span class='line'><span class="nt"><html></span>
</span><span class='line'> <span class="nt"><head></head></span>
</span><span class='line'> <span class="nt"><body></span>
</span><span class='line'> <span class="nt"><button</span> <span class="na">id=</span><span class="s">"bar"</span><span class="nt">></span>Button<span class="nt"></button></span>
</span><span class='line'> <span class="nt"><script></span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">bar</span><span class="p">);</span> <span class="c1">//Reference to <button>, even in standards mode</span>
</span><span class='line'> <span class="nt"></script></span>
</span><span class='line'> <span class="nt"></body></span>
</span><span class='line'><span class="nt"></html></span>
</span></code></pre></td></tr></table></div></figure>
<!--more-->
<h3>So What’s Going On?</h3>
<p>Believe it or not this is actually correct behavior per the HTML specification.</p>
<blockquote><p>6.2.4 Named access on the Window object</p><p>The Window interface supports named properties. The supported property names at any moment consist of:</p><p>> the value of the name content attribute for all a, applet, area, embed, form, frame, frameset, iframe, img, and object elements in the active document that have a name content attribute, and<br/>> the value of the id content attribute of any HTML element in the active document with an id content attribute.</p><footer><strong>HTML Specification</strong> <cite><a href='http://www.whatwg.org/specs/web-apps/current-work/#named-access-on-the-window-object'>www.whatwg.org/specs/web-apps/…</a></cite></footer></blockquote>
<p>What this is saying is that the value of the <code>name</code> attribute of certain elements and the value of the <code>id</code> attribute of ALL elements are accessible via the <code>window</code> object in the browser. So, if you have a node <code><button id="foo"></button></code>, then <code>window.foo</code> will be resolved to a reference to the <code><button></code>. From this point forward I will refer to this behavior as named access.</p>
<h3>How is This Standard Behavior?</h3>
<p>This behavior is an old Internet Explorer <em>feature</em>. I can only imagine that it was intended to be a convenience for web developers that got sick of typing <code>document.getElementById</code> over and over again. (this is way before jQuery and other popular toolkits came to be). Regardless of the reasoning, IE shipped with this functionality and a whole lot of people utilized it.</p>
<p>Other rendering engines were faced with the difficult decision of implementing non-standard behavior or remaining incompatible with a slew of websites written specifically for Internet Explorer.</p>
<p>Gecko implemented this functionality but originally turned it on only in quirks mode. They recently took the extra step and <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=622491">turned named access on in standards mode with Firefox 14</a>.</p>
<p>Webkit and Presto have had named access in standards mode for some time now. <a href="https://www.w3.org/Bugs/Public/show_bug.cgi?id=11960">Webkit recently considered relegating this behavior to quirks mode</a>, however, they decided on leaving it enabled in standards mode. Apparently there is still just too much stuff out there relying on this behavior to remove it in standards mode. Believe it or not Microsoft even <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=737760">shipped a marketing demo</a> that directly referenced named DOM elements, preventing it from functioning in Gecko.</p>
<p>One of the main aims of the HTML5 specification is to standardize browser behavior, however quirky it might be. Therefore, this functionality made it into the specification.</p>
<h3>Why is This Behavior Bad?</h3>
<p>I’ve alluded to the fact that this behavior is bad, but I’ve haven’t gotten into details as to why.</p>
<h4>There is a high potential for bugs to be introduced into the system</h4>
<p>Let’s say you have some code that looks something like this:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><html></span>
</span><span class='line'> <span class="nt"><head></head></span>
</span><span class='line'> <span class="nt"><body></span>
</span><span class='line'> <span class="nt"><input</span> <span class="na">type=</span><span class="s">"text"</span> <span class="na">id=</span><span class="s">"choice"</span><span class="nt">></button></span>
</span><span class='line'> <span class="nt"><script></span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">choice</span> <span class="o">=</span> <span class="s1">'foo'</span><span class="p">;</span>
</span><span class='line'> <span class="c1">//Lots more JavaScript</span>
</span><span class='line'> <span class="nx">doSomethingVeryComplicated</span><span class="p">(</span><span class="nx">choice</span><span class="p">);</span>
</span><span class='line'> <span class="nt"></script></span>
</span><span class='line'> <span class="nt"></body></span>
</span><span class='line'><span class="nt"></html></span>
</span></code></pre></td></tr></table></div></figure>
<p>Since a global <code>choice</code> variable is being created, <code>window.choice</code> will resolve to the string <code>foo</code> and not a reference to the <code><input></code>. This is because the <code><input></code> reference is being <a href="http://en.wikipedia.org/wiki/Variable_shadowing">shadowed</a> by the variable declaration. This works the same way as a variable with the same name being declared in a nested function.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">y</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="mi">3</span><span class="p">;</span>
</span><span class='line'> <span class="c1">//Logs 3 instead of 2 because the value defined in the outer</span>
</span><span class='line'> <span class="c1">//function is shadowed by the x defined in the inner function.</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">x</span><span class="p">);</span>
</span><span class='line'> <span class="p">};</span>
</span><span class='line'> <span class="nx">y</span><span class="p">();</span>
</span><span class='line'><span class="p">}());</span>
</span></code></pre></td></tr></table></div></figure>
<p>This is all well and good. However, let’s say that during a refactor of this code the <code>var choice = 'foo';</code> line is accidentally removed. Under normal circumstances this would cause a syntax error because <code>window.choice</code> would now be undefined. However, because there is a DOM node with an <code>id</code> of <code>choice</code>, that reference will now refer to the DOM node instead. This can easily lead to unexpected behavior.</p>
<p>The flip side of this situation is also true. If you have an element <code><div id="bar"></div></code> and use <code>window.bar</code> to refer to it, that code will break if you create JavaScript variable using <code>var</code> in the same scope (i.e. <code>var bar = 2;</code>).</p>
<h4>Mistyping</h4>
<p>Say you mistype the name of your variable and happen to type a named DOM element. SURPRISE!</p>
<h4>Non-trivial cost for the browser to implement</h4>
<p>In order for these named elements to be available, the browser has to create a list of all named elements and maintain it as the page changes. I can’t offer any specific metrics as to how much time and memory this takes, but there is a cost, especially on pages with a large number of named elements.</p>
<h4>Named elements will be shadowed by properties natively defined on <code>window</code>.</h4>
<p>If you <em>were</em> to go the route of using named access you’d have to be careful to avoid using named elements with values that are predefined on the <code>window</code> already.</p>
<p>For example you cannot refer to a <code><input id="location"></code> by <code>window.location</code> because that <a href="https://developer.mozilla.org/en/DOM/window.location">already resolves to an object</a> with information about the URL of the current document.</p>
<p>There are a number of other property names on the <code>window</code> object that you could easily see being used to name a DOM element - <code>event</code>, <code>history</code>, <code>name</code>, <code>self</code>, <code>status</code>, and <code>toolbar</code> to name a few.</p>
<h4>Browsers have inconsistent implementations.</h4>
<p>Even though this is behavior is now standardized, there are still browser quirks in the way named access is implemented.</p>
<h5>IE and Form Elements</h5>
<p>IE will (incorrectly) make the <code>name</code> attribute of form elements available on the <code>window</code> object.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="cp"><!DOCTYPE html></span>
</span><span class='line'><span class="nt"><html></span>
</span><span class='line'> <span class="nt"><head></head></span>
</span><span class='line'> <span class="nt"><body></span>
</span><span class='line'> <span class="nt"><form></span>
</span><span class='line'> <span class="nt"><input</span> <span class="na">name=</span><span class="s">"foo"</span> <span class="nt">/></span>
</span><span class='line'> <span class="nt"></form></span>
</span><span class='line'> <span class="nt"><script></span>
</span><span class='line'> <span class="c1">//Logs a reference to the <input> in IE.</span>
</span><span class='line'> <span class="c1">//Syntax error in all other rendering engines.</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">foo</span><span class="p">);</span>
</span><span class='line'> <span class="nt"></script></span>
</span><span class='line'> <span class="nt"></body></span>
</span><span class='line'><span class="nt"></html></span>
</span></code></pre></td></tr></table></div></figure>
<h5>Name Attribute on Anchor Tags</h5>
<p>Per the spec, <code><a></code> tags should be accessible on the <code>window</code> object via the value of their <code>name</code> attribute. However, this only works in IE and Opera.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="cp"><!DOCTYPE html></span>
</span><span class='line'><span class="nt"><html></span>
</span><span class='line'> <span class="nt"><head></head></span>
</span><span class='line'> <span class="nt"><body></span>
</span><span class='line'> <span class="nt"><a</span> <span class="na">name=</span><span class="s">"foo"</span><span class="nt">></a></span>
</span><span class='line'> <span class="nt"><script></span>
</span><span class='line'> <span class="c1">//Logs a reference to the <a> in IE and Opera.</span>
</span><span class='line'> <span class="c1">//Syntax error in Gecko and WebKit.</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">foo</span><span class="p">);</span>
</span><span class='line'> <span class="nt"></script></span>
</span><span class='line'> <span class="nt"></body></span>
</span><span class='line'><span class="nt"></html></span>
</span></code></pre></td></tr></table></div></figure>
<h5>Multiple Named Attributes with the Same Value</h5>
<p>Per this portion of the spec:</p>
<blockquote><p>…if elements has only one element, return that element and abort these steps.</p><p>Otherwise return an HTMLCollection rooted at the Document node, whose filter matches only named elements with the name name.</p><footer><strong>HTML Specification</strong> <cite><a href='http://dev.w3.org/html5/spec/single-page.html#dom-window-nameditem'>dev.w3.org/html5/spec/…</a></cite></footer></blockquote>
<p>What this is staying is that when there are multiple named properties with the same name, the browser should return an array when that property is referenced (instead of a reference to a specific DOM node). As an example given this markup:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="cp"><!DOCTYPE html></span>
</span><span class='line'><span class="nt"><html></span>
</span><span class='line'> <span class="nt"><head></head></span>
</span><span class='line'> <span class="nt"><body></span>
</span><span class='line'> <span class="nt"><input</span> <span class="na">id=</span><span class="s">"one"</span><span class="nt">></span>
</span><span class='line'> <span class="nt"><input</span> <span class="na">id=</span><span class="s">"one"</span><span class="nt">></span>
</span><span class='line'> <span class="nt"><script></span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">one</span><span class="p">);</span>
</span><span class='line'> <span class="nt"></script></span>
</span><span class='line'> <span class="nt"></body></span>
</span><span class='line'><span class="nt"></html></span>
</span></code></pre></td></tr></table></div></figure>
<p>…an array with references to the two <code><input></code> nodes should be logged per the spec. And it will be in all browsers except Firefox. Firefox 14 will simply log the first element.</p>
<p>Having two elements with the same <code>id</code> is invalid HTML, but the browser will still render it just fine. Even with the best of intentions these sorts of things do happen, especially in larger, dynamic applications.</p>
<h3>More?</h3>
<p>These are simply the bugs that I’ve ran into, I’m sure there are more. If you know of any let me know in the comments and I can update this list.</p>
<h3>But won’t strict mode prevent this?</h3>
<p><a href="https://developer.mozilla.org/en/JavaScript/Strict_mode">ECMAScript 5 strict mode</a> prevents you assigning values to variables before they are declared. Therefore this…</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">foo</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
</span><span class='line'><span class="p">}());</span>
</span></code></pre></td></tr></table></div></figure>
<p>will execute just fine whereas this…</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="s1">'use strict'</span><span class="p">;</span>
</span><span class='line'> <span class="nx">foo</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
</span><span class='line'><span class="p">}());</span>
</span></code></pre></td></tr></table></div></figure>
<p>…will produce a syntax error that <code>foo</code> is not defined. This is great, but it will not stop you from accessing named properties on the <code>window</code> object.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><div</span> <span class="na">id=</span><span class="s">"foo"</span><span class="nt">></div></span>
</span><span class='line'><span class="nt"><script></span>
</span><span class='line'> <span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="s1">'use strict'</span><span class="p">;</span>
</span><span class='line'> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">foo</span><span class="p">);</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'><span class="nt"></script></span>
</span></code></pre></td></tr></table></div></figure>
<p>This will log a reference to the <code><div></code> in standards mode in the latest version of all modern browsers. Strict mode will only prevent you from assigning values to variables that have yet to be declared. If you’re simply using a variable then strict mode doesn’t protect you. Therefore, you’re not prevented from accessing name properties on the global <code>window</code> object.</p>
<h3>What to do instead</h3>
<p>Use <code>document.getElementById</code> to retrieve references to DOM nodes via their <code>id</code> attribute.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><button</span> <span class="na">id=</span><span class="s">"foo"</span><span class="nt">></button></span>
</span><span class='line'>
</span><span class='line'><span class="nt"><script></span>
</span><span class='line'> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">);</span>
</span><span class='line'><span class="nt"></script></span>
</span></code></pre></td></tr></table></div></figure>
<p>To get a reference to a DOM node via its <code>name</code> attribute you can use <code>document.querySelectorAll</code>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><a</span> <span class="na">name=</span><span class="s">"bar"</span><span class="nt">></a></span>
</span><span class='line'>
</span><span class='line'><span class="nt"><script></span>
</span><span class='line'> <span class="nb">document</span><span class="p">.</span><span class="nx">querySelectorAll</span><span class="p">(</span><span class="s1">'[name=bar]'</span><span class="p">);</span>
</span><span class='line'><span class="nt"></script></span>
</span></code></pre></td></tr></table></div></figure>
<p><code>document.querySelectorAll</code> is not safe to use in IE <= 8, so if you need to support older IE look into using a toolkit such as <a href="http://jquery.com">jQuery</a> to select the DOM nodes that you need.</p>
<h3>Conclusion</h3>
<p>All major browsers now make named properties available on the global <code>window</code> object in standards mode. It’s important to know that browsers do this because you’ll likely run into this at some point. However, never purposely utilize this functionality. If you see others use it tell them to stop, ridicule them, do whatever it takes. Help <a href="http://movethewebforward.org/">move the web forward</a>.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Native HTML5 Number Picker and jQuery UI's Spinner - Which to Use?]]></title>
<link href="http://tjvantoll.com/2012/07/15/native-html5-number-picker-vs-jquery-uis-spinner-which-to-use/"/>
<updated>2012-07-15T21:07:00-04:00</updated>
<id>http://tjvantoll.com/2012/07/15/native-html5-number-picker-vs-jquery-uis-spinner-which-to-use</id>
<content type="html"><![CDATA[<p><a href="http://www.w3.org/TR/html5/number-state.html#number-state">HTML5’s native number picker</a> (<code><input[type=number]</code>) and jQuery UI 1.9’s spinner can both be used to create inputs for numeric data. So which makes sense for your application? Let’s start with a brief explanation of each.</p>
<h3><code>input[type=number]</code></h3>
<p>HTML5 adds several new valid <code>type</code> attributes for <code><input></code> elements. One of them, <code>number</code>, can be used to create a number picker.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><input</span> <span class="na">type=</span><span class="s">"number"</span> <span class="nt">/></span>
</span></code></pre></td></tr></table></div></figure>
<p>This will present the user with a number picker in supported browsers, which, as of this writing includes Chrome, Safari, Opera, iOS, Opera Mobile, and Android 4.0+ (<a href="http://caniuse.com/#feat=input-number">full support list</a>). Here’s what the user will see in supported browsers:</p>
<!--more-->
<h5>Chrome 20:</h5>
<p><img src="http://tjvantoll.com/images/posts/2012-07-15/Chrome.png" title="Chrome" alt="Chrome" /></p>
<h5>Safari 5.1.7:</h5>
<p><img src="http://tjvantoll.com/images/posts/2012-07-15/Safari.png" title="Safari" alt="Safari" /></p>
<h5>Opera 12.00:</h5>
<p><img src="http://tjvantoll.com/images/posts/2012-07-15/Opera.png" title="Opera" alt="Opera" /></p>
<h5>Opera Mobile 12:</h5>
<p><img alt="Opera Mobile" title="Opera Mobile" src="http://tjvantoll.com/images/posts/2012-07-15/Opera_Mobile.png" style="height: 250px;" /></p>
<h5>iOS 5:</h5>
<p><img alt="iOS" title="iOS" src="http://tjvantoll.com/images/posts/2012-07-15/iOS.png" style="height: 200px;" /></p>
<h5>Android 4.1 (Jelly Bean):</h5>
<p><img alt="Android" title="Android" src="http://tjvantoll.com/images/posts/2012-07-15/Android.png" style="height: 200px;" /></p>
<p>As you can see one of the nicest effects of using <code>[type=number]</code> is that mobile users will automatically be presented with a number pad to aid with entry of numeric data. Unsupported browsers will simply treat the <code>input[type=number]</code> as a normal text input. Firefox has <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=344616">recently added a UI-less version</a> of <code>input[type=number]</code> to their nightly builds so hopefully a fully enabled version will be coming soon.</p>
<p>You can see what your browser does below:</p>
<iframe style="width: 100%; height: 120px;" src="http://jsfiddle.net/tj_vantoll/XMEEz/1/embedded/result,html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<h4>Additional Functionality</h4>
<p>The native number picker supports <code>min</code>, <code>max</code>, and <code>step</code> attributes to allow you to pick the minimum value of the <code><input></code>, the maximum value of the <code><input></code>, and the amount the value should be incremented / decremented when the user spins through values (the <code>step</code> attribute defaults to <code>1</code> if not specified).</p>
<p>For example, on the <code><input></code> below the browser will enforce that the minimum value will be <code>2</code>, the maximum value will be <code>20</code>, and the user will step at increments of <code>2</code>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><input</span> <span class="na">type=</span><span class="s">"number"</span> <span class="na">min=</span><span class="s">"2"</span> <span class="na">max=</span><span class="s">"20"</span> <span class="na">step=</span><span class="s">"2"</span> <span class="nt">/></span>
</span></code></pre></td></tr></table></div></figure>
<p>You can see how this behaves in your browser below:</p>
<iframe style="width: 100%; height: 120px;" src="http://jsfiddle.net/tj_vantoll/YmQFS/embedded/result,html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<p>Just as a word of warning, Android 4.1 and iOS 5 do not support the <code>min</code>, <code>max</code>, or <code>step</code> attributes.</p>
<h4>Methods</h4>
<p>In addition to the new attributes, supporting browsers also provide 3 JavaScript methods specifically for <code>input[type=number]</code>.</p>
<ul>
<li><code>stepUp(n)</code> - Increment the <code>value</code> of the <code><input></code> by <code>n</code>.</li>
<li><code>stepDown(n)</code> - Decrement the <code>value</code> of the <code><input></code> by <code>n</code>.</li>
<li><code>valueAsNumber</code> - Retrieve the <code>value</code> of the <code>input</code> as a JavaScript <code>number</code> variable (by default retrieving the <code>value</code> of an <code><input></code> returns a <code>string</code>).</li>
</ul>
<h3>jQuery UI Spinner</h3>
<p>jQuery UI’s <code>spinner</code> is a new plugin due for jQuery UI’s 1.9 release (currently in beta). The plugin by default looks and behaves much like the native number picker.</p>
<iframe style="width: 100%; height: 120px;" src="http://jsfiddle.net/tj_vantoll/scXYB/1/embedded/result,js,html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<p>It also supports setting minimum, maximum, and step values through options rather than attributes.</p>
<figure class='code'><figcaption><span>jQuery UI Spinner</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt"><input</span> <span class="na">id=</span><span class="s">"spinner"</span> <span class="nt">/></span>
</span><span class='line'><span class="nt"><script></span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">$</span><span class="p">(</span><span class="s1">'#spinner'</span><span class="p">).</span><span class="nx">spinner</span><span class="p">({</span>
</span><span class='line'> <span class="nx">min</span><span class="o">:</span> <span class="mi">2</span><span class="p">,</span>
</span><span class='line'> <span class="nx">max</span><span class="o">:</span> <span class="mi">20</span><span class="p">,</span>
</span><span class='line'> <span class="nx">step</span><span class="o">:</span> <span class="mi">2</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'> <span class="p">});</span>
</span><span class='line'><span class="nt"></script></span>
</span></code></pre></td></tr></table></div></figure>
<p>Example:</p>
<iframe style="width: 100%; height: 120px;" src="http://jsfiddle.net/tj_vantoll/N7UXT/2/embedded/result,js,html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<h3>Above and Beyond</h3>
<p>What really sets jQuery UI’s <code>spinner</code> apart from the native picker is that it is extensible, customizable, and it brings a number of extra features. Here are some of the additional things that you can do.</p>
<h4>Paging</h4>
<p><code>spinner</code> takes a <code>page</code> option that allows you to define how much the <code>spinner</code> should step when the page down / page up keys are pressed. The example below shows a <code>spinner</code> with a <code>step</code> value of <code>1</code> and a <code>page</code> value of <code>10</code>.</p>
<iframe style="width: 100%; height: 150px;" src="http://jsfiddle.net/tj_vantoll/EvTeQ/1/embedded/result,js,html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<h4>Mousewheel</h4>
<p>If you want mousewheel support for a <code>spinner</code> all you need to do is include <a href="https://github.com/brandonaaron/jquery-mousewheel">Brandon Aaron’s mousewheel plugin</a> and you get it automatically! Try it out on any of the <code>spinner</code> demos on this page.</p>
<h4>Currency</h4>
<p>Ever need to accept currency at certain defined increments? This example shows a <code>spinner</code> that spins through currency values at $25 increments, all with the same clean API.</p>
<iframe style="width: 100%; height: 150px;" src="http://jsfiddle.net/tj_vantoll/2wEe6/3/embedded/result,js,html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<p>The formatting is localized through <a href="https://github.com/jquery/globalize/">Globalize.js</a>, therefore, if you want to handle different currencies all you need to do is pass in the appropriate <code>culture</code> and include the necessary JavaScript dependencies. Here’s an example of an input that takes Euros.</p>
<iframe style="width: 100%; height: 150px;" src="http://jsfiddle.net/tj_vantoll/ppH7g/embedded/result,js,html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<h4>Time</h4>
<p>If you need to accept time data <code>spinner</code> can be used for that as well.</p>
<iframe style="width: 100%; height: 150px;" src="http://jsfiddle.net/tj_vantoll/2wEe6/5/embedded/result,js,html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<p>The <code>page</code> option discussed earlier is used nicely here to make the up / down keys control the minutes and the page up / page down keys to controls hours. Try it out on the example above.</p>
<h3>24 Hour Times</h3>
<p>Since the <code>spinner</code> uses Globalize.js, you’re free to use a time system different than the United States’ nonsensical one.</p>
<iframe style="width: 100%; height: 150px;" src="http://jsfiddle.net/tj_vantoll/Kenve/2/embedded/result,js,html/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
<h3>Time Picker vs. <code><input type="time"></code></h3>
<p>HTML5 also provides a native time picker (<code>input[type=time]</code>), but, it has <a href="http://caniuse.com/#feat=input-datetime">nearly no support</a>, does not yet provide localized formatting, and does not provide the stepping/paging functionality that <code>spinner</code> has baked in. In the future it might provide a viable native solution, but for now it’s best to stay away.</p>
<h4>Extensible and Customizable</h4>
<p>Because <code>spinner</code> is built on top of <a href="http://ajpiano.com/widgetfactory">jQuery UI’s widget factory</a>, it is easily extensible. For example, let’s say you need to build an input that accepts a year in which the modern summer olympics were held. You could do that with the following:</p>
<figure class='code'><figcaption><span>Extending spinner</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="o"><</span><span class="nx">input</span> <span class="o">/></span>
</span><span class='line'>
</span><span class='line'><span class="o"><</span><span class="nx">script</span><span class="o">></span>
</span><span class='line'> <span class="nx">$</span><span class="p">.</span><span class="nx">widget</span><span class="p">(</span> <span class="s2">"tj.olympicspicker"</span><span class="p">,</span> <span class="nx">$</span><span class="p">.</span><span class="nx">ui</span><span class="p">.</span><span class="nx">spinner</span><span class="p">,</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">options</span><span class="o">:</span> <span class="p">{</span>
</span><span class='line'> <span class="nx">min</span><span class="o">:</span> <span class="mi">1896</span><span class="p">,</span>
</span><span class='line'> <span class="nx">max</span><span class="o">:</span> <span class="mi">2012</span><span class="p">,</span>