BJ Patel
BJ Patel

Reputation: 6268

Can Re-write / Modify URL with javascript / Jquery after page load

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?

Upvotes: 2

Views: 13028

Answers (5)

chalasr
chalasr

Reputation: 13167

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

Upvotes: 0

Lupo
Lupo

Reputation: 324

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

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382102

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.

Upvotes: 4

Kristoffer Sall-Storgaard
Kristoffer Sall-Storgaard

Reputation: 10636

Yes we can, but only on browsers implementing pushState.

See other answers here

Upvotes: 1

Michal Klouda
Michal Klouda

Reputation: 14521

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

document.location.hash = "whatever";

Upvotes: 6

Related Questions