Dave said:
But is there any way of setting focus to a div when a webpage loads
that is cross-browser compatible?
Apparently not. However, I double-checked your observations and could not
confirm them for Safari 3.0.4 beta (523.15), at least not with
document.links[0].focus().
document.getElementById('idname').focus() doesn't work with Safari,
That depends on what `idname' would refer to. It works fine if "idname" is
the ID of a link object, for example. [This was tested only in Safari 3.1
(525.13) on Windows XP though, released today.]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Focusing A Link On Load</title>
</head>
<body onload="document.getElementById('foo').focus()">
<h1>Focusing A Link On Load</h1>
<a href="" id="foo">bar</a>
</body>
</html>
URI:
data:text/html;charset=ISO-8859-1,%3C%21DOCTYPE%20html%20PUBLIC%20%22-//W3C//DTD%20HTML%204.01%20Transitional//EN%22%0A%20%20%22http%3A//
www.w3.org/TR/html4/loose.dtd%22%3E%0A%3Chtml%3E%0A%20%20%3Chead%3E%0A%20%20%20%20%3Cmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text/html%3B%20charset%3DISO-8859-1%22%3E%0A%20%20%20%20%3Ctitle%3EFocusing%20A%20Link%20On%20Load%3C/title%3E%0A%20%20%3C/head%3E%0A%0A%20%20%3Cbody%20onload%3D%22document.getElementById%28%27foo%27%29.focus%28%29%22%3E%0A%20%20%20%20%3Ch1%3EFocusing%20A%20Link%20On%20Load%3C/h1%3E%0A%20%20%20%20%3Ca%20href%3D%22%22%20id%3D%22foo%22%3Ebar%3C/a%3E%0A%20%20%3C/body%3E%0A%3C/html%3E
but you're right that document.links[0].focus() does work with Safari
(but it doesn't work with Opera) - thanks.
You are welcome.
Can you think of any way of fooling Opera into working properly?
No.
After all if I go to Google in Opera, the keyboard focus is in the search
box, so one would have thought what I'm trying to achieve must be
possible?
They do it with
function sf(){document.f.q.focus()}
whereas `document.f.q' proprietarily (instead of the standards-compliant as
well as backwards-compatible `document.forms["f"].elements["q"]') refers to
(pretty-printed)
<form action="/search" name=f><table cellpadding=0 cellspacing=0>
...
<input maxlength=2048 name=q size=55 title="Google Search" value="">
But AFAICS, Google Web Search does not use scrollable `div' elements. As I
pointed out, it is entirely possible in Opera (9.26) to focus elements on
load, but that does not appear to enable keyboard scrolling of scrollable
block elements.
In fact, with this approach Google prevents people from using the
keyboard for scrolling even though there is no scrollable `div' element.
They can only hope for people without pointing devices to have their
viewport large enough. And at least in Opera you can't tab to the links
below the form. Incidentally, Google code is known around here not to be
suitable as a reference for designing Web applications, to say the least.
PointedEars