Is it possible to re-write URL without reloading the page via jQuery/Javascript?
Let's say you have the following URL:
can we append the value: /pId=XYZ
So you can get
Is this possible?
Is it possible to re-write URL without reloading the page via jQuery/Javascript?
Let's say you have the following URL:
http://stackoverflow./questions/ask
can we append the value: /pId=XYZ
So you can get
http://stackoverflow./questions/ask/pId=XYZ
Is this possible?
Share Improve this question edited Jun 14, 2017 at 8:49 Malcolm Salvador 1,5663 gold badges22 silver badges41 bronze badges asked Aug 17, 2012 at 13:08 BJ PatelBJ Patel 6,27811 gold badges50 silver badges84 bronze badges 1- It's very easy to do a proper formatting of your question, maybe you could do it? – Olivier Pons Commented Aug 17, 2012 at 13:59
5 Answers
Reset to default 6All you can change without redirecting is the hash part of url:
document.location.hash = "whatever";
Yes you can, and you don't need jquery : simply use the History API : https://developer.mozilla/en-US/docs/DOM/Manipulating_the_browser_history
Beware that it's usually a little painful to handle history in a plex ajax application, as you have to handle state serialization of the pages, state loading, and so on. And you can't apply this to a badly designed site as are many ajaxified sites. And this won't work on "old" browsers like IE9.
Yes we can, but only on browsers implementing pushState.
See other answers here
you can get the current Url with
var url = document.location.href;
then you add the part to the url
url += "/mySubFolder";
rest of my post didnt fit the question ... i should read more carefully :( !
After encountering this problem, I think the @DenysSéguret answer is exactly what you need.
When it's supported by the browser, you can use
window.history.pushState(stateObj, title, url);
Or
window.history.replaceState(stateObj, title, url);
As stated by @DenysSéguret, you can find the documentation at
https://developer.mozilla/en-US/docs/Web/API/History_API