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


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}";

               leadStreet +
               leadCity +


Here's the JS Error I get:

enter image description here

Does anybody know a solution to this?

1 Answer 1



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)},";

You must log in to answer this question.