Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to rewrite url clientside with javascript without reloading page

Tags:

javascript

Is it possible to rewrite the URL in the URL-field on the client's browser?

So when a person clicks on a link on my page something ajax happens (eg a tab shows up) i want the url to display the action without refreshing the page.

Is this possible?

like image 316
richardverbruggen Avatar asked May 31 '10 15:05

richardverbruggen


People also ask

How do I use JavaScript to modify the URL without reloading the page?

the page using JavaScript? the page using JavaScript? Method 2: Adding a new state with pushState() Method: The pushState() method is used to add a new history entry with the properties passed as parameters. This will change the current URL to the new state given without reloading the page.

How do I change URL without refreshing?

history. pushState(nextState, nextTitle, nextURL); // This will replace the current entry in the browser's history, without reloading window.

Can JavaScript change the URL?

In fact, JavaScript provides the location object, a part of the window object, which allows you to perform different URL-related operations.


2 Answers

When everyone else answered this question, they were right, it wasn't possible to change the URL in javascript. With HTML5 and modern browsers, it now is using the HTML5 History API

diveintohtml5.info article about it. Here is another good blog post explaining the feature.

Check caniuse.com for browser compatability.

There are libraries such as History.js which wrap the API and pollyfill using hashbangs for old browsers.

like image 90
Tom Wadley Avatar answered Oct 04 '22 02:10

Tom Wadley


You can change the hash/anchor part of the URL (after #). The rest of the URL is readonly.

location.hash = "#value";
like image 30
Emil Vikström Avatar answered Oct 04 '22 04:10

Emil Vikström