How to remove # character from hash data? Jquery

I need the hash value from url ...

var hash = window.location.hash; 

So how do I get rid of the # sign?

+7
source share
5 answers

So simple.

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

There are also these two that return the same:

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

String.slice() was added to the specification a bit later, although this is possibly irrelevant.

The use of a replacement, as indicated below, is also an option.

None of these options give an error or warning if the window.location.hash line window.location.hash empty, so it really depends on your preference for what to use.

+11
source

You can do it -

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

Just cut out the first char:

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

You can just do

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

Note that it does not generate an error; if there is no hash in the location, it simply returns "" .

0
source

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

0
source

All Articles