For example, I have a URL as :
http://www.google.com/#hash=value2x
I want a js code to return just value2x.
I tried location.hash.split('=')[1] but that results the first hash value like if url is 
http://www.google.com/#hfh=fdg&hash=value2x
It returns fdg&hash.
I want just the value of hash.
NO jQuery Please.
Thanks for the help in advance.
function getHashValue(key) {
  var matches = location.hash.match(new RegExp(key+'=([^&]*)'));
  return matches ? matches[1] : null;
}
// usage
var hash = getHashValue('hash');
                        The URLSearchParams class can be reused for this purpose.
var urlParams = new URLSearchParams(window.location.hash.replace("#","?"));
var hash = urlParams.get('hash');
                        How about
location.hash.split('hash=')[1].split('&')[0]
This will split the hash at hash= and take the value after hash= and before any other argument .
location.parseHash = function(){
   var hash = (this.hash ||'').replace(/^#/,'').split('&'),
       parsed = {};
   for(var i =0,el;i<hash.length; i++ ){
        el=hash[i].split('=')
        parsed[el[0]] = el[1];
   }
   return parsed;
};
var obj= location.parseHash();
    obj.hash;  //fdg 
    obj.hfh;   //value2x
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With