Search code examples
javascripthtmlbrowser-historyhtml5-history

Changing the title of a page whilst using the history API?


I'm experimenting with the HTML5 history API, and I've been reading that even though you should be able to change the title of the document using it, no major browsers have yet implemented this.

The pushState function in question takes the following parameters:

history.pushState(state object, title, url);

I'm not quite sure why browsers that have implemented the above have not implemented the title aspect but that's the way things are.

My question is, if I want to change the title attribute in accordance with the history api, what is the best way to do it? Firefox at least says it may implement the above method at some point in the future:

Firefox currently ignores this parameter, although it may use it in the future.

(https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history)

This leaves me thinking anything I put in there might conflict with browsers that in the future decide to implement this.

Is there any sure fire future proof, 'SEO safe' way of doing this? Or should I just leave the pushState code as it is in the hopes that browsers will implement this at some point in the future?


Solution

  • You can set the document title with document.title:

    document.title = 'Rob says hello';
    

    As long as the URLs you're setting using the history API are accessible by direct request and those URLs are discoverable by search engine spiders (add a sitemap for those) then it shouldn't make any difference to SEO.