I am encoding a string that will be passed in a URL (via GET). But if I use escape
, encodeURI
or encodeURIComponent
, &
will be replaced with %26amp%3B
, but I want it to be replaced with %26
. What am I doing wrong?
%23 is the URL encoded representation of # . I suspect your rewrite rules will not satisfy %23 . You ought to investigate how the response is being constructed. Specifically, any URL encoding functions. However, it would be possible to solve your issue with a rewrite rule.
A space is assigned number 32, which is 20 in hexadecimal. When you see “%20,” it represents a space in an encoded URL, for example, http://www.example.com/products%20and%20services.html.
URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.
Without seeing your code, it's hard to answer other than a stab in the dark. I would guess that the string you're passing to encodeURIComponent(), which is the correct method to use, is coming from the result of accessing the innerHTML property. The solution is to get the innerText/textContent property value instead:
var str, el = document.getElementById("myUrl"); if ("textContent" in el) str = encodeURIComponent(el.textContent); else str = encodeURIComponent(el.innerText);
If that isn't the case, you can use the replace() method to replace the HTML entity:
encodeURIComponent(str.replace(/&/g, "&"));
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