How to get #hash value in a URL in JS

前端 未结 6 1971
小蘑菇
小蘑菇 2020-12-14 18:38

For example, I have a URL as :

http://www.google.com/#hash=value2x

I want a js code to return just value2x. I tried

6条回答
  •  春和景丽
    2020-12-14 19:16

    The URLSearchParams class can be reused for this purpose.

    var urlParams = new URLSearchParams(window.location.hash.replace("#","?"));
    var hash = urlParams.get('hash');
    

提交回复
热议问题