[whatwg] History: onpopstate on initial document load

Jonas Sicking jonas at sicking.cc
Thu Aug 16 00:54:19 PDT 2012


On Wed, Aug 15, 2012 at 9:17 PM, Glenn Maynard <glenn at zewt.org> wrote:
> Should this alert on initial load?
>
> <!doctype html><body onpopstate="alert('xxx')">
>
> [1] says "After creating the Document object, but before any script
> execution, certainly before the parser stops, the user agent must update
> the session history with the new page."  That invokes [2] "update the
> session history with the new page", which invokes [3] "Traverse the history
> to the new entry", which fires popstate in step 14.
>
> However, "After creating the Document object, but before any script
> execution" seems like it could happen before or after the <body> element
> has been parsed, so the alert may or may not happen.
>
> Whether it's always invoked or never invoked, it would be least confusing
> for it to match up with:
>
> <!doctype html><script>window.onpopstate = alert("xxx");</script>
>
> so either both of the above or neither of the above alert.  From what I can
> tell, this one should never alert on load, since the script doesn't run
> until after the popstate event has already happened.
>
> WebKit alerts in both of these cases (and after onload).  Firefox and Opera
> alert in neither.
>
> [1] http://www.whatwg.org/specs/web-apps/current-work/#read-html
> [2]
> http://www.whatwg.org/specs/web-apps/current-work/#update-the-session-history-with-the-new-page
> [3] http://www.whatwg.org/specs/web-apps/current-work/#traverse-the-history

The spec used to call for "popstate" to always fire during page load
(I forget exactly at which point it would fire, I believe right around
when the "load" event fire). However this was deemed both confusing
and unneccesary. Instead we introduced the history.state property
which remove the need for this.

I wrote a blog post about this here after having discussed this with
Chrome engineers:
https://hacks.mozilla.org/2011/03/history-api-changes-in-firefox-4/

It was generally accepted by all parties that this was a better
approach so it's unfortunate if the spec still calls for popstate to
be fired during pageload.

/ Jonas



More information about the whatwg mailing list