doing substring in [removed].hash

后端 未结 4 1697
渐次进展
渐次进展 2021-01-03 12:29

Somehow window.location.hash is being handled differently in different browsers. If I have a url as follows

http://maps-demo.bytecraft.com.my/postdemo/parcel         


        
4条回答
  •  时光说笑
    2021-01-03 13:06

    You could just use window.location.href instead of hash but why not use regex? More reliable and future-safe than a method based on substringing from character N. Try:

    window.location.href.match(/#parcel\/history\/(.*?)(\?|$)/)[1]
    

    Regex isn't the right answer all the time, but sometimes it's just right.

    Edit: cleaned up method encapsulation

    function GetValue()
    {
        var m = window.location.href.match(/#parcel\/history\/(.*?)(\?|$)/);
        return m ? m[1] : null;
    }
    

提交回复
热议问题