As @ujjwal-singh has already written in his answer the 2nd parameter title
may be used as a label for the entry in the history but it is optional and the browser can ignore it and decide to use a different source for the title. The note below bullet point 8 at chapter 5.5.2 of the HTML 5 specification states
The title
is purely advisory. User agents might use the title in the user interface.
This is how Chrome and Firefox work. Hence, the following code should lead to a consistent user experience among all major browsers
document.title = myTitle;
history.pushState( myState, myTitle, myURL );
This code also updates the title of the document and Chrome and Firefox are supposed to use this one.
However, it does not work as expected and you will experience a weird off-by-one bug. (This is at least true for FF up to version 45 and Chrome 51.) The problem is that the UI is not updated immediately but only after the JS call stack becomes empty. Hence, pushState
will still use the old (out-dated) title, because the new title of the document is not yet applied to the DOM. On the next call of this code snippet pushState
uses the previously changed title and so on.
If the user picks an item from the history, the user is mislead to a different page than the user expects, because the labels of each history entry and the actual destination of the history entry are out-of-sync by one.
However, this looks like a bug in Chrome and Firefox to me. Although the UI is only updated after JS terminates pushState
should already use the new title. I have filed a bug report both to Chromium and Mozilla.
document.title = "NEW_TITLE"
before a call tohistory.pushstate
but why does it not work the way its supposed to? – Bullringtitle
param is a dummy? – Bullringtitle
param? – Bullring