I need value of hash from url...
var hash = window.location.hash;
So how do I get rid of #
sign?
I need value of hash from url...
var hash = window.location.hash;
So how do I get rid of #
sign?
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.
You can do this -
hash = hash.replace(/^#/, '');
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 ""
.
window.location.href.substr(0, window.location.href.indexOf('#'))
will do the trick