[whatwg] Setting document.location.hash to a not-yet-loaded element

Consider the following test page:

<!doctype html>
<title>test</title>
<script>document.location = "#frag"</script>
<div style="margin-top: 100em"></div>
<p id=frag>Jump to me!</p>

Observed behavior in both Chrome 4 and Opera 9.6 is that the browser
jumps to the given fragment; Firefox 3.5 does not.  I believe all
versions of IE jump to the fragment as well, since MediaWiki relies on
this, although I haven't personally tested.

The issue is, of course, that at the time document.location is set, no
element with id "frag" exists in the document.  However, IMO it's
expected that setting a fragment should cause the same behavior as if
the user had navigated to the document with that fragment to begin
with -- namely, that the UA jumps to the fragment when it loads.

The current text of the specification appears to support Firefox's behavior:

"When the user agent is required to scroll to the fragment identifier,
it must change the scrolling position of the document, or perform some
other action, such that the indicated part of the document is brought
to the user's attention. If there is no indicated part, then the user
agent must not scroll anywhere."
http://www.whatwg.org/specs/web-apps/current-work/#scroll-to-fragid

I believe this is wrong, and both the spec and Firefox should change.
If there is no indicated part, but the document hasn't yet fully
loaded, then the user agent should check whether each newly-added
element is the indicated part, and jump to the first one that is.
This behavior also appears to match the majority of browsers, if I'm
right about IE.

Received on Wednesday, 2 September 2009 22:11:56 UTC