0

I have a url that looks like this

http://foo.com/Item/newItemName

After some operation on the page the item gets saved to the database with the item name. Now I want to replace the url like this

http://foo.com/Item/12345

I need to update the window state, so is it possible to update the url using window.history.replaceState method or something similar to that?

user3587180
  • 1,317
  • 11
  • 23

3 Answers3

2

What you are looking for is the pushState function.

Check these docs out: https://developer.mozilla.org/en-US/docs/Web/API/History_API

Also, check out this tutorial: https://css-tricks.com/using-the-html5-history-api/

The pushState function is super handy.

miguelsolano
  • 519
  • 3
  • 11
0

Should be able to do something like:

history.pushState({}, null, newUrl);

Where newUrl is the new url which should replace the current one. Note this is HTML5 only.

datatype_void
  • 433
  • 5
  • 23
0

I would use replaceState, and not pushState, because you don't want to create a new entry in the user's browser history.

history.replaceState(
  '{ foo: "bar" }', 
  'Some new title', 
  'http://foo.com/Item/12345'
);
Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61