Firefox 自动解码 url 中的编码参数,在 IE 中不会发生这种情况

发布于 2024-10-14 14:30:48 字数 666 浏览 1 评论 0原文

我在 Firefox 和 IE 之间感到沮丧,尤其是 Firefox,因为它会自动解码哈希中的参数,然后我才能在 Javascript 中使用它。 IE 不会自动解码 url,因此不会给我读取错误。

我的问题与此类似,只是我没有使用 ASP.NET ASP.NET MVC 自动解码来自 AJAX 的 JSON 编码参数

因此,如果我采用像 example.com/#question=!%40%23%24%25^%26*(

而“!%40%23%24%25^%26*(”是使用encodeURIComponent编码的,在IE中,当我访问哈希时,它将保留为“!%40%23%24%25^” %26*(",但是在 Firefox 中,当我访问哈希时,它会自动解码为 "!@#$%^&*("

问题在于,在我的脚本中,我使用decodeURIComponent 来解码编码值,如果字符串确实已编码,那就没问题了,因为它已经在 Firefox 中进行了解码,所以它会给出格式错误的 URI 序列错误,而 IE 根本不会给出任何错误,

我该如何解决这个问题?

I am having frustration between Firefox and IE, well mostly Firefox as it is automatically decoding a parameter in the hash before I can work with it in Javascript. IE does not automatically decode the url thus not giving me reading errors.

My problem is similar to this one except I am not using ASP.NET ASP.NET MVC automatically decoding JSON-encoded parameters from AJAX

So if I take a url like example.com/#question=!%40%23%24%25^%26*(

whereas the "!%40%23%24%25^%26*(" was encoded using encodeURIComponent, in IE when I access the hash it will be left as "!%40%23%24%25^%26*(", however in firefox, when I access the hash it is automatically decoded into "!@#$%^&*("

The problem with this is that in my script I am using decodeURIComponent to decode the encoded value, which is fine if the string is indeed encoded. Since it is already decoded in Firefox, it gives me a malformed URI sequence error, and IE does not give me any errors at all.

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云柯 2024-10-21 14:30:48

经过搜索我发现这是一个跨浏览器问题,最好使用 location.href.split("#")[1] 而不是 window.location.hash< /代码>

After searching I found out that this is a cross browser problem, and it is better to use location.href.split("#")[1] instead of window.location.hash

虚拟世界 2024-10-21 14:30:48

这实际上是您想要使用的:

decodeURI(window.location.hash.substr(1))

确实 window.location.href.split("#!")[1] 不会被 FF 自动解码(至少在今天)。

This is actually what you want to use:

decodeURI(window.location.hash.substr(1))

Indeed window.location.href.split("#!")[1] does not get decoded by FF automatically (at least today).

不再见 2024-10-21 14:30:48

这是一个非常老的问题,但根本问题仍然没有解决。 Firefox 对其他浏览器不编码的内容进行了编码。

出于沮丧,我不得不创建一种完全不同的方法,并且实际上使算法独立于字符串是否被编码。

我希望这个解决方案能找到需要它的人:

function encodeOnce(text) {
  var doubleEncoded = encodeURIComponent(text);
  // only dive into it if there are any encoded strings...
  if (doubleEncoded.indexOf('%') != -1) {
    // reverse replace all % signs
    doubleEncoded = doubleEncoded.replace(/%25/g, '%');
    // if this is not equal to the original string, ...
    if (doubleEncoded != text) {
      // ... that means there was something to encode
      text = doubleEncoded;
    }
  }
  return text;
}

那么你可以这样做:

solution = encodeOnce(window.location.hash.slice(1));

你觉得怎么样?

This is a really old question, but the underlying problem is still not solved. Firefox encodes something that other browsers don't.

Out of frustration, I had to create an entirely different approach and actually make the algorithm independent of whether the string was encoded or not.

I hope this solution finds those who need it:

function encodeOnce(text) {
  var doubleEncoded = encodeURIComponent(text);
  // only dive into it if there are any encoded strings...
  if (doubleEncoded.indexOf('%') != -1) {
    // reverse replace all % signs
    doubleEncoded = doubleEncoded.replace(/%25/g, '%');
    // if this is not equal to the original string, ...
    if (doubleEncoded != text) {
      // ... that means there was something to encode
      text = doubleEncoded;
    }
  }
  return text;
}

So then you can do this:

solution = encodeOnce(window.location.hash.slice(1));

What do you think?

浮世清欢 2024-10-21 14:30:48

上面的答案有效,除非您的网址包含多个 # 的情况。这应该可以处理所有情况:

var hash = "";
var indexOfHash = location.href.indexOf("#");
if (indexOfHash > -1) {
    hash = location.href.substring(indexOfHash);
}

此外,似乎这个问题应该很快就会在 Firefox 中得到修复。只需播放晚间节目即可:

https://bugzilla.mozilla.org/show_bug.cgi?id= 378962

The answer above works except for cases where your url contains more than one #. This should handle all cases:

var hash = "";
var indexOfHash = location.href.indexOf("#");
if (indexOfHash > -1) {
    hash = location.href.substring(indexOfHash);
}

Also, it seems like this should be fixed in Firefox soon. Just hit the nightlies:

https://bugzilla.mozilla.org/show_bug.cgi?id=378962

一紙繁鸢 2024-10-21 14:30:48

我遇到了这个问题。我用这个解决方案解决了它:

var currentLocation = document.location.hash;
var decodedLocation = decodeURI(currentLocation);

I had this problem. I solved it with this solution:

var currentLocation = document.location.hash;
var decodedLocation = decodeURI(currentLocation);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文