Firefox 自动解码 url 中的编码参数,在 IE 中不会发生这种情况
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
经过搜索我发现这是一个跨浏览器问题,最好使用
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 ofwindow.location.hash
这实际上是您想要使用的:
确实 window.location.href.split("#!")[1] 不会被 FF 自动解码(至少在今天)。
This is actually what you want to use:
Indeed window.location.href.split("#!")[1] does not get decoded by FF automatically (at least today).
这是一个非常老的问题,但根本问题仍然没有解决。 Firefox 对其他浏览器不编码的内容进行了编码。
出于沮丧,我不得不创建一种完全不同的方法,并且实际上使算法独立于字符串是否被编码。
我希望这个解决方案能找到需要它的人:
那么你可以这样做:
你觉得怎么样?
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:
So then you can do this:
What do you think?
上面的答案有效,除非您的网址包含多个 # 的情况。这应该可以处理所有情况:
此外,似乎这个问题应该很快就会在 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:
Also, it seems like this should be fixed in Firefox soon. Just hit the nightlies:
https://bugzilla.mozilla.org/show_bug.cgi?id=378962
我遇到了这个问题。我用这个解决方案解决了它:
I had this problem. I solved it with this solution: