2

Is it possible to re-write URL without reloading the page via jQuery/Javascript?

Let's say you have the following URL:

http://stackoverflow.com/questions/ask

can we append the value: /pId=XYZ

So you can get

http://stackoverflow.com/questions/ask/pId=XYZ

Is this possible?

Malcolm Salvador
  • 1,476
  • 2
  • 21
  • 40
BJ Patel
  • 6,148
  • 11
  • 47
  • 81

5 Answers5

6

All you can change without redirecting is the hash part of url:

document.location.hash = "whatever";
Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
4

Yes you can, and you don't need jquery : simply use the History API : https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history

Beware that it's usually a little painful to handle history in a complex 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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

Yes we can, but only on browsers implementing pushState.

See other answers here

Community
  • 1
  • 1
Kristoffer Sall-Storgaard
  • 10,576
  • 5
  • 36
  • 46
1

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 :( !

Lupo
  • 324
  • 1
  • 3
  • 14
0

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.org/en-US/docs/Web/API/History_API

chalasr
  • 12,971
  • 4
  • 40
  • 82