Reputation:
I need value of hash from url...
var hash = window.location.hash;
So how do I get rid of #
sign?
Upvotes: 8
Views: 17493
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
Reputation: 195
window.location.href.substr(0, window.location.href.indexOf('#'))
will do the trick
Upvotes: 0
Reputation: 28511
Just cut out the first char:
var hash = window.location.hash.slice(1);
Upvotes: 1
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