---
title: "Firefox中使用location.hash会自动decodeURI Bug"
author: "zhouyrt"
date: "2014-06-03T11:26:59Z"
tags: []
categories: ["Web前端"]
slug: "firefox-location-hash-decodeuri-bug"
draft: false
lastmod: "2014-06-03T11:26:59Z"
id: 2075374
url: "https://snandy.iteye.com/blog/2075374"
---

用一中文单词测试，比如“昌平” encodeURI后为“%E6%98%8C%E5%B9%B3”。

在各浏览器地址栏输入: http://start.firefoxchina.cn/#%E6%98%8C%E5%B9%B3

 

从控制台获取hash，各浏览器表现如下

 

**Firefox21**

**![](http://dl2.iteye.com/upload/attachment/0097/7352/aeb7b5d4-e154-300f-a573-73e9d6c25d51.png)**  
 

**Chrome27/Safari5**

**![](http://dl2.iteye.com/upload/attachment/0097/7354/5a1f3486-f371-3997-91ae-eb0ed7306b49.png)**  
 

**IE9/10/11**

**![](http://dl2.iteye.com/upload/attachment/0097/7356/d90413df-f014-3533-be80-ef53f1d291ae.png)**  
 

可以看见，只有Firefox中把#后的decodeURI了，这在Firefox的地址栏也可以看见。这应该是个bug，如果所有浏览器要保持一致（未decodeURI），使用以下代码

```
function getHash(location) {
    var match = location.href.match(/#(.*)$/);
    return match ? match[1] : '';
}
```

 

相关：

[http://xueduany.github.io/KitJs/urlencode.html](http://xueduany.github.io/KitJs/urlencode.html)