Liam_Rab3
Liam_Rab3

Reputation: 35

Completely remove hash from URL

I was wondering how to take away the id from the url. Like so:

Link to page:

http://www.exampledomain.com/index.html#example

When user click the link it becomes:

http://www.exampledomain.com/index.html

Upvotes: 0

Views: 122

Answers (1)

Blazemonger
Blazemonger

Reputation: 92983

There are several approaches to this, depending on your specific context.

If you just want to remove the hash value:

    location.hash = '';

...but this leaves the # in the location. (It also scrolls the user to the top of the page.) To remove that:

    location = location.pathname;

...but this will reload the page, too. To solve all these problems:

    history.pushState({},'',location.pathname); 
    // back button returns to #example

or

    history.replaceState({},'',location.pathname); 
    // back button doesn't return to #example

...which isn't supported in some old browsers (including IE 9), but those are vanishing rapidly.

Upvotes: 1

Related Questions