How to remove hash (#) from Url?

0

I generate a hash with this code but I can not remove it.

$('#terror').on('click', function() {
    window.location.hash='/terror';
});

And I get this URL:

http://localhost:8080/#/terror

I want to remove it using this code:

 $('#cTodos').on('click', function() {
   window.location.hash ='';
 });

And I get this URL:

 http://localhost:8080/#

I need to leave it like this:

 http://localhost:8080
    
asked by Sixto Mujica 18.03.2017 в 21:25
source

2 answers

0

You can not, if you set an empty string to location.hash what will happen is that what is after the hash will be deleted but it will remain intact. If you set a new value for window.location the page will be reloaded:

window.location.hash = '';

The solution is simply to replace the URL through the History API:

history.replaceState({}, null, '/');
    
answered by 18.03.2017 / 21:55
source
1

As explained in MDN, "manipulating browser history" , you can use pushState of the history API.

window.history.pushState({}, document.title, window.location.pathname);

You must bear in mind that although this does not irrigate the page, if the user reloaded the page with F5 the browser will go to the new changed URL and not to the original one.

    
answered by 18.03.2017 в 21:54