-
Notifications
You must be signed in to change notification settings - Fork 14
/
viewport.asp
457 lines (359 loc) · 16.3 KB
/
viewport.asp
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
<%If Request.QueryString("quirks") <> "1" Then %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><%end if%>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="en-us">
<title>V is for Viewport</title>
<link rel="home" href="mylib.html">
<link rel="up" href="mylib.html">
<link rel="previous" href="size.html" title="S is for Size">
<link rel="stylesheet" type="text/css" media="all" href="style/mylib.css">
<link rel="stylesheet" type="text/css" media="all" href="style/tester.css">
<style type="text/css" media="all">
body { text-align:center }
#indicator {
border:none;
padding:0;
position:absolute;
background-color:#000;
color:#fff;
left:0;
top:0;
overflow:hidden
}
#indicator div {
padding:1em
}
#indicator legend {
background-color:#000;
color:#fff;
}
p, pre, h1, h2, h3, fieldset, ul {
text-align:left;
}
</style>
<style type="text/css" media="print">
#indicator, fieldset {
display:none !important
}
pre {
overflow:visible;
overflow-y:visible;
}
</style>
</head>
<body onload="if (typeof bodyLoad == 'function') { bodyLoad(); }">
<h1>V is for Viewport</h1>
<p><%If Request.QueryString("quirks") = "1" Then%>
You are testing in <em>quirks</em> mode. Test in <a href="viewport.asp">standards mode</a>.
<%Else%>
You are testing in <em>standards</em> mode. Test in <a href="viewport.asp?quirks=1">quirks mode</a>.
<%End If%></p>
<script type="text/javascript">
if (typeof window.document.getElementById != 'undefined' && typeof window.document.documentElement.style.display == 'string') {
window.document.write('<div id="indicator" style="display:none"><div>When the document is scrolled to its origin, this <code>DIV<\/code> should cover the client area of the viewport <em>exactly<\/em>.<fieldset><legend>Test<\/legend><input type="button" onclick="if (testResize) testResize()" value="Resize"><input type="button" onclick="window.document.getElementById(\'indicator\').style.display = \'none\'" value="Close"><\/fieldset><\/div><\/div>');
}
var testResize, bodyLoad = function() {
var getViewportDimensions, getRoot, scrollbarHeight, scrollChecks;
var doc = window.document, html = doc.documentElement;
// NOTE: Assumes height of horizontal scroll bar is equal to the width of the vertical
if (typeof doc.createElement != 'undefined') {
scrollbarHeight = (function() {
var height = 0, elDivOuter = doc.createElement('div');
elDivOuter.style.width = '5px';
elDivOuter.style.overflow = 'auto';
elDivOuter.style.visibility = 'hidden';
elDivOuter.style.position = 'absolute';
elDivOuter.style.top = '0';
var elDivInner = doc.createElement('div');
elDivInner.style.width = '200px';
elDivInner.style.height = elDivOuter.style.height = '100px';
elDivOuter.appendChild(elDivInner);
elDivOuter.style.overflowX = 'auto';
doc.body.appendChild(elDivOuter);
if (typeof elDivOuter.offsetHeight == 'number' && typeof elDivOuter.clientHeight == 'number' && elDivOuter.offsetHeight > elDivOuter.clientHeight) {
height = elDivOuter.offsetHeight - elDivOuter.clientHeight;
}
doc.body.removeChild(elDivOuter);
return height;
})();
// Test to find which element contains the changing clientWidth/Height properties
if (html && typeof doc.createElement != 'undefined') {
// Possible ambiguity between HTML and body
scrollChecks = (function() {
var oldBorder, body = doc.body, result = { compatMode: doc.compatMode };
var clientHeight = html.clientHeight, bodyClientHeight = body.clientHeight;
var elDiv = doc.createElement('div');
elDiv.style.height = '100px';
body.appendChild(elDiv);
result.body = !clientHeight || clientHeight != html.clientHeight;
result.html = bodyClientHeight != body.clientHeight;
body.removeChild(elDiv);
if (result.body || result.html && (result.body != result.html)) {
// found single scroller--check if borders should be included
// skipped if body is the real root (as opposed to just reporting the HTML element's client dimensions)
if (typeof body.clientTop == 'number' && body.clientTop && html.clientWidth) {
oldBorder = body.style.borderTopWidth;
body.style.borderTopWidth = '0px';
result.includeBordersInBody = body.clientHeight != bodyClientHeight;
body.style.borderTopWidth = oldBorder;
}
return result;
}
})();
}
}
// Try to figure the outer-most rendered element.
// NOTE: This element may not hold the proper clientWidth/Height properties
if (typeof doc.compatMode == 'string') {
getRoot = function(win) {
var doc = win.document, html = doc.documentElement, compatMode = doc.compatMode;
return (html && compatMode.toLowerCase().indexOf('css') != -1 && html.clientWidth) ? html : doc.body;
};
} else {
getRoot = function(win) {
var doc = win.document, html = doc.documentElement;
return (!html || html.clientWidth === 0) ? doc.body : html;
};
}
// NOTE: Do not pass bogus window objects to these functions
if (typeof doc.clientWidth == 'number') {
// If the document itself implements clientWidth/Height (e.g. Safari 2)
// This one is a rarity. Coincidentally KHTML-based browsers reported to implement
// these properties have trouble with clientHeight/Width as well as innerHeight/Width.
getViewportDimensions = function(win) {
if (!win) {
win = window;
}
var doc = win.document;
return [doc.clientWidth, doc.clientHeight];
};
} else if (html && typeof html.clientWidth == 'number') {
// If the document element implements clientWidth/Height
getViewportDimensions = function(win) {
if (!win) {
win = window;
}
var root = getRoot(win), doc = win.document;
var clientHeight = root.clientHeight, clientWidth = root.clientWidth;
// Replace previous guess at root container
if (scrollChecks) {
root = scrollChecks.body ? doc.body : doc.documentElement;
}
clientHeight = root.clientHeight;
clientWidth = root.clientWidth;
if (scrollChecks && scrollChecks.body && scrollChecks.includeBordersInBody) {
// Add body borders
clientHeight += doc.body.clientTop * 2;
clientWidth += doc.body.clientLeft * 2;
}
return [clientWidth, clientHeight];
};
} else if (typeof window.innerWidth == 'number') {
// If the window implements innerWidth/Height
if (html && typeof html.scrollWidth == 'number' && typeof html.clientWidth == 'number' && scrollbarHeight) {
// Adjusted for scroll bars
// *** NOTE: branch orphaned now (remove as not needed)
getViewportDimensions = function(win) {
if (!win) {
win = window;
}
var root = getRoot(win);
return [win.innerWidth - ((root.scrollHeight > root.clientHeight && win.innerWidth > scrollbarHeight) ? scrollbarHeight : 0), win.innerHeight - ((root.scrollWidth > root.clientWidth && win.innerHeight > scrollbarHeight) ? scrollbarHeight : 0)];
};
} else {
// Last resort, return innerWidth/Height
// NOTE: May include space occupied by scroll bars
getViewportDimensions = function(win) {
if (!win) {
win = window;
}
return [win.innerWidth, win.innerHeight];
};
}
}
// Test
if (typeof doc.getElementById != 'undefined') {
var el = doc.getElementById('indicator');
if (el) {
el.style.display = 'none'
if (getViewportDimensions) {
testResize = function() {
var root = getRoot(window);
el.style.display = 'none';
var clientHeight = root.clientHeight;
var d = getViewportDimensions();
el.style.display = '';
el.style.width = d[0] + 'px';
el.style.height = d[1] + 'px';
// IE8 screwiness, setting width to container clientWidth can cause a 1px overflow,
// creating a horizontal scroll bar.
// Next line is a mystical incantation (appears to allow IE to recalculate the container clientWidth)
(root.clientHeight);
if (clientHeight > root.clientHeight) {
el.style.width = (d[0] - 0.1) + 'px';
}
};
} else {
el = doc.getElementById('testbutton');
if (el) {
el.disabled = true;
}
}
} else {
el = doc.getElementById('testbutton');
if (el) {
el.disabled = true;
}
}
}
// Discard unneeded host objects
doc = html = null;
};
if (typeof window.document.getElementById != 'undefined' && typeof window.document.documentElement.style.display == 'string') {
window.document.write('<fieldset><legend>Test<\/legend><input id="testbutton" type="button" onclick="window.document.getElementById(\'indicator\').style.display = \'\';testResize()" value="Open Indicator"><\/fieldset>');
}
</script>
<h2>IE Has it Right</h2>
<p>Measuring the client area of the viewport is very easy in MSHTML. The viewport is actually an element. Which one? The Big One With the Scroll Bar. We need the area inside the border, excluding space taken up by scroll bars. MS invented the <code><a href="http://msdn.microsoft.com/en-us/library/ms533566(VS.85).aspx">clientWidth</a>/Height</code> properties for just this purpose.
<h3>Finding the Right Element</h3>
<p>In MSHTML, the HTML element (<code>document.documentElement</code>) is used for the viewport in standards mode and the body in quirks (HTML is not rendered). In IE6-8, the <code><a href="http://msdn.microsoft.com/en-us/library/ms533687(VS.85).aspx">document.compatMode</a></code> property indicates the rendering mode. This property does not exist in IE < 6, but quirks mode can be detected easily enough as we know that the HTML element is not rendered in quirks mode. Inspecting the HTML element's <code>clientWidth/Height</code> properties reveals they are 0 in quirks mode. A document is more likely to legitimately have an 0 pixel height than width, so we will check the <code>clientWidth</code> property.</p>
<pre>
// This code works for all IE versions and modes
var getRoot, getViewportDimensions;
if (typeof doc.compatMode == 'string') {
getRoot = function(win) {
var doc = win.document, html = doc.documentElement, compatMode = doc.compatMode;
return (html && compatMode.toLowerCase().indexOf('css') != -1) ? html : doc.body;
};
} else {
getRoot = function(win) {
var doc = win.document, html = doc.documentElement;
return (!html || html.clientWidth === 0) ? doc.body : html;
};
}
getViewportDimensions = function(win) {
if (!win) {
win = window;
}
var root = getRoot(win);
return [root.clientWidth, root.clientHeight];
};
</pre>
<h2>The Trouble with the Others</h2>
<p>The history of this critical implementation is a comedy of errors.</p>
<h3>Tried to Copy IE</h3>
<p>Over the years, the other major browsers have copied IE, but there have been troubles along the way. For one, the browser developers observed that the properties needed to measure the client area of the viewport moved between the HTML and body elements when the rendering mode was switched, but apparently did not correlate this with the MSHTML rendering behavior. In quirks mode, the HTML element <em>is</em> typically rendered, but its rightful (per MS) <code>clientHeight/Width</code> properties are reported by the body element. This makes no logical sense, but doesn't hinder the initial rendition as the <code>document.compatMode</code> property still indicates the proper element <em>in most cases</em>.</p>
<h3>Opera Fouled Up Worse</h3>
<p>Prior to 9.5, Opera used the body <em>exclusively</em> to report what should be the HTML <code>clientHeight/Width</code>, which makes even less sense and requires feature testing to work around, with the body's border(s) figuring in when present. As of 9.5, they have caught up to the other major non-IE browsers.</p>
<h3>The Window innerHeight/Width Properties</h3>
<p>More often than not, especially in modern browsers released in the last five years, these properties include space occupied by scroll bars. Therefore we refer to these properties only as a last resort.</p>
<pre>
// Last resort, return innerWidth/Height
// NOTE: May include space occupied by scroll bars
getViewportDimensions = function(win) {
if (!win) {
win = window;
}
return [win.innerWidth, win.innerHeight];
};
</pre>
<h3>Safari 2 Oddity</h3>
<p>Some older KHTML-based browsers (e.g. Safari 2) feature <code>document.clientHeight/Width</code> properties. These properties are arguably a better alternative to measuring elements, but are apparently no longer in production.</p>
<pre>
// This code will work in Safari 2 and other older KHTML-based browsers
getViewportDimensions = function(win) {
if (!win) {
win = window;
}
var doc = win.document;
return [doc.clientWidth, doc.clientHeight];
};
</pre>
<h2>Putting it All Together</h2>
<p>We need a feature test to find the correct element.</p>
<pre>
var scrollChecks, html = doc.documentElement;
if (html && typeof doc.createElement != 'undefined') {
// Test to resolve ambiguity between HTML and body
scrollChecks = (function() {
var oldBorder, body = doc.body, result = { compatMode: doc.compatMode };
var clientHeight = html.clientHeight, bodyClientHeight = body.clientHeight;
var elDiv = doc.createElement('div');
elDiv.style.height = '100px';
body.appendChild(elDiv);
result.body = !clientHeight || clientHeight != html.clientHeight;
result.html = bodyClientHeight != body.clientHeight;
body.removeChild(elDiv);
if (result.body || result.html && (result.body != result.html)) {
// found single scroller--check if borders should be included
// skipped if body is the real root (as opposed to just reporting the HTML element's client dimensions)
if (typeof body.clientTop == 'number' && body.clientTop && html.clientWidth) {
oldBorder = body.style.borderWidth;
body.style.borderWidth = '0px';
result.includeBordersInBody = body.clientHeight != bodyClientHeight;
body.style.borderWidth = oldBorder;
}
return result;
}
})();
}
</pre>
<p>With this and the <code>getRoot</code> function, we can create a simple wrapper, choosing between 4 (5 in the longer version) different algorithms.</p>
<pre>
var doc = window.document, html = doc.documentElement;
if (typeof doc.clientWidth == 'number') {
// If the document itself implements clientWidth/Height (e.g. Safari 2)
// This one is a rarity. Coincidentally KHTML-based browsers reported to implement
// these properties have trouble with clientHeight/Width as well as innerHeight/Width.
getViewportDimensions = function(win) {
if (!win) {
win = window;
}
var doc = win.document;
return [doc.clientWidth, doc.clientHeight];
};
} else if (html && typeof html.clientWidth == 'number') {
// If the document element implements clientWidth/Height
getViewportDimensions = function(win) {
if (!win) {
win = window;
}
var root = getRoot(win), doc = win.document;
var clientHeight = root.clientHeight, clientWidth = root.clientWidth;
// Replace previous guess at root container
if (scrollChecks) {
root = scrollChecks.body ? doc.body : doc.documentElement;
}
clientHeight = root.clientHeight;
clientWidth = root.clientWidth;
if (scrollChecks && scrollChecks.body && scrollChecks.includeBordersInBody) {
// Add body borders
clientHeight += doc.body.clientTop * 2;
clientWidth += doc.body.clientLeft * 2;
}
return [clientWidth, clientHeight];
};
} else if (typeof window.innerWidth == 'number') {
// Last resort, return innerWidth/Height
// NOTE: May include space occupied by scroll bars
getViewportDimensions = function(win) {
if (!win) {
win = window;
}
return [win.innerWidth, win.innerHeight];
};
}
// Discard unneeded host object references
doc = html = null;
</pre>
<p>This can be simplified further for contexts where body borders are not used.</p>
<p>See the source for the complete rendition. It is based on a method from <a href="http://www.cinsoft.net/mylib.html">My Library</a>.</p>
<p><a href="http://www.jibbering.com/faq/#getWindowSize">CLJ FAQ article on the same subject</a></p>
<h2>Other Primers</h2>
<ul><li><a href="attributes.html">A is for Attributes</a></li><li><a href="host.html">H is for Host</a></li><li><a href="keyboard.html">K is for Keyboard</a></li><li><a href="position.html">P is for Position</a></li><li><a rel="previous" href="size.html">S is for Size</a></li></ul>
<div id="legal">© 2007-2009 by <a href="mailto:[email protected]">David Mark</a></div>
<div><a href="mylib.html" id="home" title="Home"><img src="images/logo.gif" height="20" width="22" alt="Home"></a></div>
<address><a href="mailto:[email protected]">[email protected]</a></address>
</body>
</html>