Safari 中 url 中的特殊字符 [英] Special characters in url in Safari

查看:124
本文介绍了Safari 中 url 中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

如果在 Safari 中像 window.location.hash = encodeURI("вап"); 一样设置哈希值,它可以工作,但当然它在 Chrome、FF 和其他人中不起作用.

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

推荐答案

我终于找到了解决方案.如果通过 window.location.href 设置哈希,一切正常.

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

代码如下:

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;
}

这篇关于Safari 中 url 中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆