Why do you use the history.pushState function?

11

I noticed that the history object has some relation to browser history manipulation. There are methods like go and back which are more understandable, but I do not quite understand what the pushState does.

What is the purpose of history.pushState ?

    
asked by anonymous 16.10.2017 / 15:33

1 answer

10
  

How to build websites that take advantage of the principles of AJAX, while still being accessible to search engines?

     

Response: A Javascript function that is part of the HTML5 API History, called window.history.pushState ().   The pushState function of the javascript History object is to change the url of the browser without giving Refresh. Change the url without reloading the page,

Whenever you open a new tab and / or window, the browser starts a new session. And it's in this session that it stores all the URLs you've visited.

The pushState method records a new entry in your session history, keeping history. And that's your syntax:

window.history.pushState(data, title [, url ] )

data : The data parameter can be useful if you want to use the onPopState event, which is invoked whenever a new entry is logged in your session history;

Title : This is the title of the page you want the entry to have;

URL : This is the URL you want the page to have. You can use this parameter in two ways:

  • Absolute: Passing the entire new URL, including protocol, host, path etc. Ex: link ;
  • Relative: The URL you pass will be relative to the current URL, that is, if you are accessing the link and passing " / category / javascript / "the URL that will be registered is" link ".

Example:

window.history.pushState('Object', 'Categoria JavaScript', '/category/javascript/');

Conclusion

Now with HTML5 we can create sites with AJAX indexable by search engines without problems. Our Link Building will not be compromised and the user experience can be further improved, since only a fraction of the code delivered is actually the content.

example - JSFiddle

another example

    
16.10.2017 / 17:23