2

I'm using a merge field inside a piece of JavaScript Code.

{!Lead.Street}

However in some cases, the result can be an address with 2 lines, and the newline character (I suspect) is causing an illegal character exception in my JavaScript.

Here's my current JS:

<apex:repeat value="{!AllLeads}" var="lead">
    var leadStreet = "{!lead.Street},"
    var leadCity = "{!lead.City},";
    var leadCountry = "{!lead.Country}";

        address.push( 
               leadStreet +
               leadCity +
               leadCountry
            );

</apex:repeat>

Here's the JS Error I get:

enter image description here

Does anybody know a solution to this?

3

Use JSENCODE

Encodes text and merge field values for use in JavaScript by inserting escape characters, such as a backslash (), before unsafe JavaScript characters, such as the apostrophe (').

{!JSENCODE(text)} and replace text with the merge field or text string that contains the unsafe JavaScript characters.

var leadStreet = "{!JSENCODE(lead.Street)},"
var leadCity = "{!JSENCODE(lead.City)},";

Your Answer

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