Retrieve specific hash tag's value from url

前端 未结 4 1875
误落风尘
误落风尘 2020-12-13 16:59

In raw Javascript, how would one go about checking that a specific hash tag exists in a url, then grab the value?

Example: http://www.example.com/index.html#hashtag1

4条回答
  •  渐次进展
    2020-12-13 17:19

    I use this, and it works just fine for me. It's a little adjusing to a line I picked up somewhere, I believe on SO.

    getURLHashParameter : function(name) {
    
            return decodeURI(
                (RegExp('[#|&]' + name + '=' + '(.+?)(&|$)').exec(location.hash)||[,null])[1]
            );
        }, 
    

提交回复
热议问题