/
index.html
683 lines (476 loc) · 37.9 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
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>DOM Element References as Global Variables - TJ VanToll - Tutorials, Thoughts, and Ramblings on Front End Development</title>
<meta name="author" content="TJ VanToll">
<meta name="description" content="Quiz: What is logged when the following markup is rendered? 1
2
3
4
5
6
7
8
9
<html> <head></head> <body> <button id=" …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://tjvantoll.com/2012/07/19/dom-element-references-as-global-variables">
<!-- Use custom favicon -->
<!--<link href="/favicon.png" rel="icon">-->
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<!--Custom script concatenation
<script src="/javascripts/modernizr-2.5.3.js"></script>
<script src="/javascripts/ender.js"></script>
<script src="/javascripts/octopress.js"></script>
<script src="/javascripts/PictureCube.js"></script>
<script src="/javascripts/ender.tipsy.js"></script>
<script src="/javascripts/blog.js"></script>
-->
<script src="/javascripts/build/scripts.js"></script>
<link href="/atom.xml" rel="alternate" title="TJ VanToll" type="application/atom+xml">
<style>
/* Fonts from Google"s Web font directory at http://google.com/webfonts */
@import url(http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic);
@import url(http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic);
</style>
<script type="text/javascript">
if (window.location.host.indexOf('local') == -1) {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-29179796-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
}
</script>
</head>
<body >
<header role="banner"><hgroup>
<div id="picture-cube"></div>
<h1><a href="/">TJ VanToll</a></h1>
<h2>Tutorials, Thoughts, and Ramblings on Front End Development</h2>
</hgroup>
</header>
<nav role="navigation"><ul class="subscription" data-subscription="rss">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="q" value="site:tjvantoll.com" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul class="main-navigation">
<li><a href="/blog">Blog</a></li>
<li><a href="/">About</a></li>
<li><a href="/blog/archives">Archives</a></li>
<li><a href="/speaking">Speaking</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div>
<article class="hentry" role="article">
<header>
<h1 class="entry-title">DOM Element References as Global Variables</h1>
<p class="meta">
<time datetime="2012-07-19T22:33:00-04:00" pubdate data-updated="true">Jul 19<span>th</span>, 2012</time>
| <a href="#disqus_thread">Comments</a>
</p>
</header>
<div class="entry-content"><p>Quiz: What is logged when the following markup is rendered?</p>
<figure class='code'><figcaption class='empty'><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><a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/ReferenceError">ReferenceError</a> 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 class='empty'><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 class='empty'><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 class='empty'><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 ReferenceError 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 class='empty'><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">//ReferenceError 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 class='empty'><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">//ReferenceError 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 class='empty'><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 class='empty'><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 class='empty'><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 ReferenceError 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 class='empty'><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 class='empty'><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.getElementsByName</code> or <code>document.querySelectorAll</code>.</p>
<figure class='code'><figcaption class='empty'><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"><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">getElementsByName</span><span class="p">(</span><span class="s1">'bar'</span><span class="p">);</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, but <code>document.getElementsByName</code> is safe to use in all browsers.</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>
<h3>Update September 24th, 2012</h3>
<p>Per comments from Rob W I’ve updated the following:</p>
<ul>
<li>I had incorrectly used “Syntax error” to describe the situation when an undeclared variable is referenced. I updated those to correctly use “ReferenceError”.</li>
<li>I updated my last example to show that <code>document.getElementsByName</code> is a cross browser way of getting reference to DOM nodes by their name attribute.</li>
</ul>
</div>
<footer>
<p class="meta">
<span class="byline author vcard">Posted by <span class="fn">TJ VanToll</span></span>
<time datetime="2012-07-19T22:33:00-04:00" pubdate data-updated="true">Jul 19<span>th</span>, 2012</time>
<span class="categories">
<a class='category' href='/blog/categories/browsers/'>Browsers</a>, <a class='category' href='/blog/categories/javascript/'>JavaScript</a>
</span>
</p>
<p class="author_card meta">
<img src="/images/me/1.jpg" alt="Me!" id="post_me" />
TJ VanToll is an avid web developer working for <a href="http://liquidweb.com">Liquid Web</a> and living in Lansing, MI. He is an open source advocate who publishes this blog <a href="https://github.com/tjvantoll/tjvantoll.github.com/tree/source">open source</a> and contributes to <a href="http://jqueryui.com">jQuery UI</a>. When not on the internet he is generally found <a href="/images/kids/2.jpg">brainwashing his children</a>, debating CSS with his :wife, or talking about himself in the third person. If you like hearing TJ talk about himself you should consider <a href="http://twitter.com/tjvantoll">following him on Twitter</a>.
</p>
<div class="sharing">
<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://tjvantoll.com/2012/07/19/dom-element-references-as-global-variables/" data-via="tjvantoll" data-counturl="http://tjvantoll.com/2012/07/19/dom-element-references-as-global-variables/" >Tweet</a>
</div>
<p class="meta">
<a class="basic-alignment left" href="/2012/07/15/native-html5-number-picker-vs-jquery-uis-spinner-which-to-use/" title="Previous Post: Native HTML5 Number Picker and jQuery UI's Spinner - Which to Use?">« Native HTML5 Number Picker and jQuery UI's Spinner - Which to Use?</a>
<a class="basic-alignment right" href="/2012/08/05/html5-form-validation-showing-all-error-messages/" title="Next Post: HTML5 Form Validation - Showing All Error Messages">HTML5 Form Validation - Showing All Error Messages »</a>
</p>
</footer>
</article>
<section>
<h1>Comments</h1>
<div id="disqus_thread" aria-live="polite"><noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</section>
</div>
<aside class="sidebar">
<section>
<h1>Recent Posts</h1>
<ul id="recent_posts">
<li class="post">
<a href="/2012/12/12/debugging-print-stylesheets-now-significantly-easier-in-chrome/">Debugging Print Stylesheets: Now Significantly Easier in Chrome</a>
</li>
<li class="post">
<a href="/2012/11/10/creating-cross-browser-scrollable-tbody/">Creating Cross Browser Scrollable <tbody>s - A CSS Only Approach</a>
</li>
<li class="post">
<a href="/2012/11/08/creating-a-polished-vertical-tabs-interaction-with-jquery-ui/">Creating a Polished Vertical Tabs Interaction with jQuery UI</a>
</li>
<li class="post">
<a href="/2012/10/24/adding-pointers-to-jquery-ui-tooltips/">Adding Pointers to jQuery UI Tooltips</a>
</li>
<li class="post">
<a href="/2012/10/23/front-end-efficiency-the-top-ten/">Front End Efficiency - The Top Ten</a>
</li>
<li class="post">
<a href="/2012/10/17/maxlength-constraint-validation-oddities/">maxlength Constraint Validation Oddities in Major Browsers</a>
</li>
<li class="post">
<a href="/2012/09/19/using-the-attribute-selector-with-numeric-values/">Using the Attribute Selector with Numeric Values</a>
</li>
<li class="post">
<a href="/2012/09/14/using-jquery-ui-slider-to-polyfill-html5-input-type-range/">Using jQuery UI's Slider to Polyfill HTML5's input[type=range]</a>
</li>
<li class="post">
<a href="/2012/08/30/html5-form-validation-start-using-it-in-production-applications/">HTML5 Form Validation - Start Using it in Production Applications</a>
</li>
<li class="post">
<a href="/2012/08/28/google-analytics-excluding-visits-in-development-and-production/">Google Analytics - Excluding Your Own Visits in Development and Production</a>
</li>
</ul>
</section>
<section>
<h1>Latest Tweets</h1>
<ul id="tweets">
<li class="loading">Status updating...</li>
</ul>
<script type="text/javascript">
$.domReady(function(){
getTwitterFeed("tjvantoll", 4, false);
});
</script>
<script src="/javascripts/twitter.js" type="text/javascript"> </script>
<a href="http://twitter.com/tjvantoll" class="twitter-follow-button" data-show-count="false">Follow @tjvantoll</a>
</section>
<section class="jsdocs">
<h1><a href="http://promotejs.com">Promote JS!</a></h1>
<a href="https://developer.mozilla.org/en/JavaScript/Guide" title="JS Tutorial, JavaScript Tutorial, JavaScript Guide, Learn JavaScript JS, How To Learn JS, Learning JavaScript"><img src="http://static.jsconf.us/promotejshs.png" height="150" alt="JS Tutorial, JavaScript Tutorial, JavaScript Guide, Learn JavaScript JS, How To Learn JS, Learning JavaScript" width="180"></a></section>
</aside>
</div>
</div>
<footer role="contentinfo"><p>
Copyright © 2012 - <a href="/">TJ VanToll</a> -
<span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
</p>
</footer>
<script type="text/javascript">
var disqus_shortname = 'tjvantoll';
// var disqus_developer = 1;
var disqus_identifier = 'http://tjvantoll.com/2012/07/19/dom-element-references-as-global-variables/';
var disqus_url = 'http://tjvantoll.com/2012/07/19/dom-element-references-as-global-variables/';
var disqus_script = 'embed.js';
(function () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/' + disqus_script;
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
}());
</script>
<script type="text/javascript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
</script>
</body>
</html>