0

So I have the following url:

https://example.com/page/foo

And I'm using pushstate like this:

window.history.pushState(window.location.href, null, 'product/'+data.name+'/'+data.item);

Now, the problem is that when it changes the url, it removes "foo" so the new url will be

https://example.com/page/product/product-name/123

and I don't want this, I want to keep the original url...and append the new stuff

Uffo
  • 9,628
  • 24
  • 90
  • 154

2 Answers2

2

The chosen answer did not work for me, and I found a better alternative. So in order to keep the "base url" and replace everything after just use:

window.history.pushState(null, null, '/product/'+data.name+'/'+data.item);

That is, simple precede your path with a slash (/)

TrampGuy
  • 1,737
  • 1
  • 13
  • 9
1

Use this instead

window.history.pushState(null, null, window.location.href + 'product/' + data.name + '/' + data.item);

Wilmer
  • 138
  • 1
  • 8