Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change URL link with javascript without refresh

Is it possible to automatically change the url example.com/4000/title-2/#!4000 to example.com/4000/title-2 without to refresh the page ? Basically to remove "/#!4000" from the URL. Note that is important to remove the "/" before the hashbang not just the hashbang .

like image 234
themihai Avatar asked Apr 21 '12 17:04

themihai


People also ask

How do I change URL but not reload?

There is no way to modify the URL in the browser without reloading the page. The URL represents what the last loaded page was. If you change it ( document. location ) then it will reload the page.

Can we change URL using JavaScript?

location to Redirect to a Different URL with JavaScript. You can redirect a web page to another page in a number of ways including server-side redirects, HTML meta refresh redirects and JavaScript redirects.

How do I change a dynamic URL?

Ideally pushState() is used to change the URL seen in the browser's address bar; while popstate is executed once a user hits 'Back' in their browser. The example below will simply modify the URL of the given page. However, this method can only modify locations within the same domain.

Which statement can a developer apply to increment the navigation's history without page refresh?

pushState() method # The history. pushState() method can be used to push a new entry into the browser's history—and as a result, update the displayed URL—without refreshing the page.


1 Answers

dont know if it is enough for you and whether it works completely cross-browser... chrome accepts:

location.hash = "";

but this keeps the "#" in the address bar

in modern browsers that completely support the html5 history api you do:

window.history.replaceState('Object', 'Title', '/4000/title-2');

EDIT: this dies not change the history of the browser

EDIT 2: just found this stackoverflow resource

like image 138
Tobias Krogh Avatar answered Sep 28 '22 03:09

Tobias Krogh