function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
thpthp 

How to avoid URL encoding of ampersand

Just a quick question: How do I avoid urlencoding an ampersand (the '&' character). I wish to make a URI with parameters - the parameters are separated by ampersands, but they get encoded automatically (i.e., '&' goes to '&'). 

 

The code looks like

 

<apex:iframe src="https://some-domain.com/action?a={!value1}&b={!value2}"/>

 

where value1 and value2 are attributes on my controller. The ampersand in between the two parameters is causing me trouble...

 

I would have expected that only application of the URLENCODE function would result in this substitution, but this is not the case. The '&' character is substituted without it.

 

All help is appreciated. Thank you!

Ranjeet Singh (SFDC Developer)Ranjeet Singh (SFDC Developer)

Use EncodingUtil.urlEncode(url, 'UTF-8'); method.

This code will help you to resolve your issue.

 

Try:

String str1 = EncodingUtil.urlEncode(Value1, 'UTF-8');

String str2 = EncodingUtil.urlEncode(Value2, 'UTF-8');

and pass str1 and str2 in url then it will work find.

 

because you are passing the value1 or value with the combination of & that why you are getting the eroor message.

 

sfdcfoxsfdcfox

Actually, that is the correct means of encoding an ampersand in HTML. Your problem lies elsewhere.

 

Proof:

 

<a href="https://www.google.com/search?safe=off&amp;q=test">Google.com</a>

Google.com

 

Try my link above, you'll see that the &amp; is properly decoded into an & when your browser goes to the page.

 

Edit: So, like, I should probably say, you're NOT encoding value1 nor value2. Depending on the contents of those values, that would cause your grief.

 

Edit 2: The forums are junking up my code. But the fact is, you're supposed to use character entities for ampersand, quote, less-than, and greater-than in any url in HTML.