Encoding of [removed].hash

前端 未结 4 1847
死守一世寂寞
死守一世寂寞 2020-12-13 02:22

Does window.location.hash contain the encoded or decoded representation of the url part?

When I open the same url (http://localhost/something/#%C3

4条回答
  •  死守一世寂寞
    2020-12-13 02:27

    Unfortunately, this is a bug in Firefox as it decodes location.hash an extra time when it is accessed. For example, try this in Firefox:

    location.hash = "#%30";
    location.hash === "#0"; // This is wrong, it should be "#%30"
    

    The only cross-browser solution is to just use (location.href.split("#")[1] || "") instead for getting the hash. Setting the hash using location.hash seems to work correctly for all browsers that support location.hash though.

提交回复
热议问题