Adarsha Beleyur
Adarsha Beleyur

Reputation: 157

How to remove Hashtag in URL using jquery

I want to remove the hashtag in my URL. For instance

http://localhost/Tech/technology/#method

In the above URL I need to remove # tag and the result will be like this:

http://localhost/Tech/technology/method

Can you help me please?

Upvotes: 1

Views: 1742

Answers (3)

I.Manev
I.Manev

Reputation: 727

First get the url, replace # with '' and reload:

$(document).ready(function() {
    var url = window.location.href;
    url.replace('#', '');
    location.href = url;
});

Upvotes: 0

TARUN KUMAR
TARUN KUMAR

Reputation: 141

Use encodeURIComponent it will percent encode your URL string. ex:

var set4 = "ABC abc 123"; // Alphanumeric Characters + Space

console.log(encodeURIComponent(set4)); // ABC%20abc%20123 (the space gets encoded as %20)

It will solve not only # but for all special characters.

Upvotes: 1

PraveenKumar
PraveenKumar

Reputation: 1861

You can do that by using javascript.

document.location.href = String( document.location.href ).replace( /#/, "" );

var url = "http://localhost/Tech/technology/#method"
url = String( url ).replace( /#/, "" );
console.log(url);
alert(url);

Upvotes: 0

Related Questions