4

我们在 Web 应用程序中使用了一些特殊字符,如下所示:example.com/foo#вап.

decodeURI(window.location.hash)我们使用(有时哈希包含未编码的特殊字符)解析哈希并设置新值,如window.location.hash = "вап".

在 Chrome、Firefox、Opera 甚至 IE 中一切正常,但在 Safari 中我们得到20?вап.

如果在 Safari 中设置哈希值,window.location.hash = encodeURI("вап");它可以正常工作,但当然它在 Chrome、FF 等中不起作用。

4

2 回答 2

5

最后我找到了解决方案。如果通过设置散列window.location.href一切正常。

这是代码:

var newHash = ...
var sharpIdx = window.location.href.indexOf("#");
if (sharpIdx === -1) {
  window.location.href = window.location.href + "#" + newHash;
} else {
  window.location.href = window.location.href.substr(0, sharpIdx) + "#" + newHash;
}
于 2013-04-07T11:06:16.740 回答
0

我遇到了同样的问题,找到了另一种解决方法

window.location.hash = path;
// For safari url change fix
if (window.location.hash !== path) {
    window.location.hash = encodeURI(path);
}
于 2017-03-07T09:12:49.110 回答