[SalesForce] getParameters query with variable that has white space.

I have a world map in a Visualforce page and once a country is clicked on, then the action is that the user is auto-navigated to another Visualforce page (/apex/Google_Maps_Reports) which has a parameter passed to the URL.

On this Visualforce page, I have a method to the controller on that page which returns a bar chart of all Opportunities where the Account.BillingCountry is the name of the parameter passed to the URL.

In the examples, Canada is working but United States is not. I believe the latter is not working because the URL returns pc0=United+States. Is there any nice code snippet I can use to rectify this or will I have to create a custom formula field on the Account object where all values are one word, and use that instead?

<apex:page >

<script type='text/javascript' src='http://www.google.com/jsapi'></script>
<script type='text/javascript'>google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawVisualization);

function drawVisualization() {var data = new google.visualization.DataTable();

 data.addColumn('string', 'Country');
 data.addColumn('number', 'Value'); 
 data.addColumn({type:'string', role:'tooltip', p:{html:true}});var ivalue = new Array();

 data.addRows([[{v:'CA',f:'canada'},2,'']]);
 **ivalue['CA'] = 'https://cs81.salesforce.com/apex/Google_Maps_Reports_Overview?pc0=canada';**

 data.addRows([[{v:'US',f:'canada'},2,'']]);
 ivalue['CA'] = 'https://cs81.salesforce.com/apex/Google_Maps_Reports_Overview?pc0=United States';

Best Answer

You can encode the space (and other characters that have special meaning in a URL) in the JavaScript by using encodeURIComponent:

ivalue['CA'] = 'https://cs81.salesforce.com/apex/Google_Maps_Reports_Overview?pc0='
        + encodeURIComponent('United States');

or in the Apex by using EncodingUtil.urlEncode.