Firefox automatically decoding encoded parameter in url, does not happen in IE

删除回忆录丶 提交于 2019-11-29 16:57:15

问题


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?


回答1:


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




回答2:


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).




回答3:


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?




回答4:


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




回答5:


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

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


来源:https://stackoverflow.com/questions/4835784/firefox-automatically-decoding-encoded-parameter-in-url-does-not-happen-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!