0

Iam forming a URL in JavaScript and passing this URL on click of a custom button,i want to encode the parameters, some of the parameters having value like &, I tried with string.replace() and encodeURI..

var url = '/apex/GSDCSCAccountLocationSearch?srmid=' + '{!pse__Proj__c.Id}'+
    '&cmpname=' +'{!pse__Proj__c.pse__Account__r.name}' +
    '&city=' +'{! pse__Proj__c.PSA_Location_City__c}' +
    '&state=' +'{! pse__Proj__c.PSA_Location_State__c}' +
    '&zip=' +'{!pse__Proj__c.PSA_Location_Zip__c}'+
    '&renderSaveButton=TRUE'+'&country='+CountryCode; 
1

You're merging from Visualforce, so you need to encode before it gets merged into the code. Use JSINHTMLENCODE to get your parameters properly encoded. Also, since you're merging the values directly into your code, there's no need for all that string concatenation. Finally, you could probably just use URLFOR to get your URL properly formatted.

var url = '{!JSINHTMLENCODE(URLFOR($Page.GSDCSCAccountLocationSearch, null, [srmid=pse__Proj__c.Id, cmpname= pse__Proj__c.pse__Account__r.name, city= pse__Proj__c.PSA_Location_City__c, state= pse__Proj__c.PSA_Location_State__c, zip= pse__Proj__c.PSA_Location_Zip__c, renderSaveButton=TRUE]))}& country='+CountryCode;

This should take care of all the relevant escaping for you.

  • tried the above code getting the syntax error.Error: Syntax error. Found '$Page.' – srini Nov 22 '15 at 4:10
  • @srini My phone inserted an extra space in $Page.GSDCSCAccountLocationSearch (after the period). I've fixed this. – sfdcfox Nov 22 '15 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.