Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL encode sees “&” (ampersand) as “&” HTML entity

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?

like image 974
dododedodonl Avatar asked Aug 22 '10 13:08

dododedodonl


People also ask

What does %23 mean in a URL?

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

What does %20 in a URL mean?

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.

What does %20 replace in URL?

URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.


1 Answers

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, "&")); 
like image 153
Andy E Avatar answered Oct 04 '22 04:10

Andy E