Change the URL with JavaScript without redirecting<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Change the URL with JavaScript without redirecting

Posted on: August 26, 2015

If you have a part of your website that need to change the URL in the browser without having to refresh the whole page, than you shouldn't use the window.location.href because this one will redirect into that page. window.location contains 2 others method that can do the job for you. The first one is assign.window.location.assign(url) is a method that will change the url and set in your history the new url. Sometime, we want to browser to change but not the history. A second method does that job -- it's called replace.

On the other hand, doing that mean that the user can navigate directly into your webpage by the url in the browser. If you changed the URL by just changing a hash tag that define a specific action in your website, you may have that action trigged by the click event but not when the user access the webpage directly from the url.

For example, you have several link for action like "open item1" and "open item2". You change the url with window.location.assign because you want the user to be able to use the back button -- use the history. So the history looks like this:

If someone go directly to the item2 than you need to execute the code behind this hash tag. To have your code executed you need to use the window event called onhashchange.

1window.onhashchange = function() {
2 //Get the hash value from the url and execute your code

The onhashchange become a hub if all time the url change which can be convenient to have a central place has a content controller. This method removes the need to have action handled in every click that has hash url to just have the click event of these links to change the url and let the onhashchange handles which action to execute.

Here is an example:

1<a id="report">Current Order Report</a>
2 <a id="edit">Edit Current Order</a>
2 window.location.assign("#open-current-report");
6 window.location.assign("#edit-order");
9window.onhashchange = function(){
10 if(window.location.hash) {
11 var hashFragment = window.location.hash.substring(1); // Get hash value (without #)
12 switch(hashFragment) {
13 case "open-current-report":
14 //Do your action break;
15 case "edit-order":
16 //Do your action break;
17 }
18 }

This example use JQuery to attach the click event but this is not required for the location assign, neither for the onhashchange -- they are both pure JavaScript method and event.