Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page title is not changed by history.pushState

I've just opened a blank HTML page with some little amount of base tags (like html, body, head, etc) in Google Chrome, and tried to execute the following command in console:

history.pushState(null, 'my test title', '/test/url'); 

History events work fine, but the page title stays unchanged. Is it OK? Should I change it manually every time? If I should, why there is such parameter in pushState() method like title?

like image 233
avasin Avatar asked Dec 19 '12 15:12

avasin


People also ask

What is history pushState?

In an HTML document, the history. pushState() method adds an entry to the browser's session history stack.

Does history pushState reload page?

But this function is not intended to reload the browser. All the function does, is to add (push) a new "state" onto the browser history, so that in future, the user will be able to return to this state that the web-page is now in.

What is the difference between pushState and replaceState?

The big difference is, that while pushState will create a new entry in the browser's history, replaceState will only replace the current state. As a side effect of this, using the replaceState method will change the URL in the address bar, without creating a new history entry.


2 Answers

It seems current browsers don't support pushState title attribute. You can easily achieve the same thing by setting it in JS.

document.title = "This is the new page title."; 
like image 72
Kirill Avatar answered Sep 30 '22 07:09

Kirill


Setting the title using document.title is not recommended if you want good SEO.

You may want to consider using History.js

History.js gracefully supports the HTML5 History/State APIs (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState. Supports jQuery, MooTools and Prototype.

Demo

Source

like image 26
Rahul Desai Avatar answered Sep 30 '22 06:09

Rahul Desai