user796443
user796443

Reputation:

How do I remove # sign from hash data? jquery?

I need value of hash from url...

var hash = window.location.hash;

So how do I get rid of # sign?

Upvotes: 8

Views: 17493

Answers (5)

Adil Shaikh
Adil Shaikh

Reputation: 44740

You can do this -

hash = hash.replace(/^#/, '');

Upvotes: 6

MildlySerious
MildlySerious

Reputation: 9180

As easy as that.

var hash = window.location.hash.substr(1)

There are also these two which return the exact same:

var hash = window.location.hash.slice(1)
var hash = window.location.hash.substring(1)

String.slice() was added to the spec a little later, although that's propably unimportant.

Using replace as mentioned below is an option, too.

None of those options throw an error or warning if the window.location.hash string is empty, so it really depends on your preferences what to use.

Upvotes: 13

Peter Schoep
Peter Schoep

Reputation: 195

window.location.href.substr(0, window.location.href.indexOf('#')) will do the trick

Upvotes: 0

flavian
flavian

Reputation: 28511

Just cut out the first char:

 var hash = window.location.hash.slice(1);

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382274

You can simply do

 var hash = window.location.hash.slice(1);

Note that it doesn't produce an error if there is no hash in the location, it just returns "".

Upvotes: 0

Related Questions