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?
kinda de-facto standard yes. but only in modern browsers. its done for user convienience, so you can put utf8 charactesr in an url and its still pretty to the human eye. however please be aware that the text is actually still encoded and will be transmittet/requested encoded, it is only displayed decoded.
First, most browsers URL encode any special characters in the URL, so if you type in < in a URL, the browser converts it to %3C (as required by RFC 1738 Section 2.2).
URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. For example, when embedded in a block of text to be transmitted in a URL the characters < and > are encoded as %3c and %3e.
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
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).
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With