小编nme*_*aly的帖子

有没有办法让firefox不自动url-decode分配给document.location.hash的值?

我正在使用document.location.hash来保留页面上的状态,我将url编码的键值对放在那里,用"&"字符分隔.到现在为止还挺好.

但是我在Firefox上遇到了一个恼人的问题 - Firefox会在路上悄悄地对哈希值进行url-decode,所以当你稍后解决它时它会被解码.

我可以通过检测我在firefox上运行的时间以及在路上两次调用encodeURIComponent来解决问题,但显然这很可怕,我真的不想这样做.

这是一个简单的例子,我将"="编码为"%3D",将其放入哈希值,当我稍后将其删除时,它会自动转回"=":

// on the way in::
document.location.hash = "foo=" + encodeURIComponent("noisy=input");

//then later.....
// on the way out: 
var hash = document.location.hash;
kvPair = hash.split("=");

if (kvPair.length==2) {
    console.log("that is correct.")
} else if (kvPair.length==3) {
    console.log("oh hai firefox, this is incorrect")
}
Run Code Online (Sandbox Code Playgroud)

我的手指交叉,有一些隐藏的DOM元素,firefox创建,代表实际(未解码)哈希值?

但是底线 - 有没有人碰到这个并找到一个更好的解决方案,而不仅仅是浏览器检测并在Firefox上调用encodeURIComponent两次?

注意:我认为其他几个问题具有相同的根本原因.最值得注意的是这一个:

/sf/ask/338422661/

javascript url encoding

11
推荐指数
1
解决办法
3474
查看次数

标签 统计

encoding ×1

javascript ×1

url ×1