Firefox自动解码url中的编码参数,不会在IE中发生 [英] Firefox automatically decoding encoded parameter in url, does not happen in IE
问题描述
我的问题类似于这个,除了我不使用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不会给我任何错误。
我如何解决这个问题?
搜索后我发现这是一个跨浏览器问题,最好使用 location.href.split(#)[1]
而不是 window.location.hash
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?
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
这篇关于Firefox自动解码url中的编码参数,不会在IE中发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!