forked from mislav/diveintohtml5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input-autofocus-with-fallback-document-ready.html
52 lines (44 loc) · 1.62 KB
/
input-autofocus-with-fallback-document-ready.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="robots" content="noindex" />
<title><input autofocus> with jQuery fallback - Dive Into HTML 5</title>
<!--[if lt IE 9]><script src="../j/html5.js"></script><![endif]-->
<script src="../j/jquery.js"></script>
<script>
$(document).ready(function() {
if (!("autofocus" in document.createElement("input"))) {
$("#q").focus();
}
});
</script>
<link rel="stylesheet" href="style-html5.css" />
<meta name=viewport content='initial-scale=1.0' />
</head>
<body>
<form>
<input id="q" autofocus>
<input type="submit" value="Go">
</form>
<pre><code><head>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
if (!("autofocus" in document.createElement("input"))) {
$("#q").focus();
}
});
</script>
</head>
<body>
<form name="f">
<input id="q" autofocus>
<input type="submit" value="Go">
</form>
</body>
</code></pre>
<p>jQuery fires its custom <code>ready</code> event as soon as the page <abbr>DOM</abbr> is available — that is, it waits until the page text is loaded, but it doesn’t wait until all the images are loaded. This is not an optimal approach — if the page is unusually large or the network connection is unusually slow, a user could still start interacting with the page before your focus script executes. But it is still far better than waiting until the <code>window.onload</code> event fires.
<p><a href=../forms.html#more-on-focus>← back to Dive Into HTML 5</a>
</body>
</html>