Javascript 寻找Firefox的解决方案';s错误的location.hash读取,可用于所有有效的片段值

Javascript 寻找Firefox的解决方案';s错误的location.hash读取,可用于所有有效的片段值,javascript,firefox,Javascript,Firefox,Firefox(但不是Chrome)会弄乱位置。哈希结果: 什么变通方法适用于所有有效的片段值 不是这个,请注意: <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <scri

Firefox(但不是Chrome)会弄乱位置。哈希结果:

什么变通方法适用于所有有效的片段值

不是这个,请注意:

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script>
"use strict";
location.hash = "'%97";

console.log(location.hash);
// Expected: #'%97
// SUCCESS on Chrome
// FAIL on Firefox: #%27%97

// Attempted fix
console.log('#'+decodeURIComponent(location.href.split("#")[1] || ""));
// FAIL 'Uncaught URIError: URI malformed'
</script>

“严格使用”;
location.hash=““%97”;
console.log(location.hash);
//应为:#“%97”
//铬的成功
//在Firefox上失败:#%27%97
//试图修复
console.log(“#”+decodeURIComponent(location.href.split(“#”)[1]| |“”);
//“未捕获URI错误:URI格式错误”失败
注意:这不是一个重复的问题,该问题的答案是错误的

更新:上述示例的%97在注释中声明为无效。下面是一个避免该问题的示例:

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script>
"use strict";
location.hash = "%20'";

console.log(location.hash);
// Expected: #%20'
// SUCCESS on Chrome
// FAIL on Firefox: #%20%27

// Attempted fix
console.log('#'+decodeURIComponent(location.href.split("#")[1] || ""));
// FAIL: # '
</script>

“严格使用”;
location.hash=“%20';
console.log(location.hash);
//应为:#%20'
//铬的成功
//在Firefox上失败:#%20%27
//试图修复
console.log(“#”+decodeURIComponent(location.href.split(“#”)[1]| |“”);
//失败:#'
注意:标题以前是:“我如何解决Firefox的错误位置。散列读取?”,更改为澄清

注:Kaido以下2017-01-10的回答不符合适用于所有有效哈希值的要求,但符合不太严格的要求,如其评论中所述


注意:此错误报告是相关的,pic

try
unescape
insead of
decodeURIComponent
decodeURIComponent中的实际错误是因为
%97
是无效的URIsequence@JaromandaXunescape已弃用。解决方案:使用有效的
位置。散列
。“它谈论的是百分比编码字符。。。但是%97不是这些编码字符中的任何一个。“它包括在‘编码字符百分比(一个百分比后跟两个十六进制数字)’下的%97。%97是有效的编码字符百分比。具体地说,它是未保留的编码字符百分比。未保留的事实并不意味着编码无效。”。