8

I need value of hash from url...

var hash = window.location.hash;

So how do I get rid of # sign?

  • possible duplicate of [What is the difference between slice() and substr() in JavaScript?](http://stackoverflow.com/questions/4546284/what-is-the-difference-between-slice-and-substr-in-javascript) – Danubian Sailor May 14 '13 at 09:48
  • 1
    @alex23 relax. I still cannot approve answer, 1 min remaining. –  May 14 '13 at 09:55

5 Answers5

13

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.

MildlySerious
  • 8,750
  • 3
  • 28
  • 30
6

You can do this -

hash = hash.replace(/^#/, '');
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
1

Just cut out the first char:

 var hash = window.location.hash.slice(1);
flavian
  • 28,161
  • 11
  • 65
  • 105
0

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 "".

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

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

Peter Schoep
  • 195
  • 6