Special characters in url in Safari

2019-04-09 18:35发布

We are using some special characters in our web application like this: example.com/foo#вап.

We parse hash using decodeURI(window.location.hash) (and sometimes hash contains not encoded special characters) and set new value like window.location.hash = "вап".

Everything works fine in Chrome, Firefox, Opera and even IE, but in Safari we get 20? instead вап.

If set hash in Safari like window.location.hash = encodeURI("вап"); it works, but of course it doesn't work in Chrome, FF and others.

2条回答
何必那么认真
2楼-- · 2019-04-09 18:41

Finally I found the solution. If set hash through window.location.href everything works fine.

Here is the code:

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;
}
查看更多
爷的心禁止访问
3楼-- · 2019-04-09 18:43

I have faced same issue, found another workaround

window.location.hash = path;
// For safari url change fix
if (window.location.hash !== path) {
    window.location.hash = encodeURI(path);
}
查看更多
登录 后发表回答