[SalesForce] Escaping characters within AMPscript /Javascript hybrid

We have added a javascript calendar to a page otherwise coded within ExactTarget using AMPscript.The calendar is fullcalendar.js and was impressed how easily it worked to create our lists of events. Challenges of course appeared only when connecting to real data where parenthesis used in a text description field are being seen in the javascript as function indicators and breaking the script (ie. no calendar appearing). I'll be honest that I have very little experience in breaking characters and don't know where to start.

My guess here is that somewhere within the javascript I'll need to insert something that indicates "When you read this "(" replace with "(" but that's a ballpark guesstimate. I may also post to a fullcalendar.js zone in Stack but didn't see a lot of activity there.

$('#calendar').fullCalendar({
eventSources: [
{
events: [ // put the array in the `events` property
%%[
FOR @i = 1 to RowCount(@rows) do
SET @row = Row(@rows, @i)
SET @evID = Field (@row, "evID")
SET @Name = Field(@row, "Name")...
]%%
            {
                title  : '%%=v(@Name)=%%',
    url: 'http://pages.info.site.com/details/?name=%%=v(@Name)=%%&eventGUID=%%=v(@eventGUID)=%%'
            },
            %%[ next @i ]%%
        ],

    }

Now if the VAR set for @Name has a (parenthesis) in it, when the code is brought to the client browser it obviously will interpret that as part of the javascript and fails. Might be other alternatives such as encoding the text field? As always hoping for something easy to comprehend and implement but not expecting that has to be the case.

I think this is the JS object representing an event below.

{
title  : 'What's new in Software (SELECTdetails 4)',
start  : '6/26/2014 12:00:00 AM',
url: 'http://pages.info.events.com/details/?name=What's new in Software (SELECTdetails 4)?&eventGUID=dfb1d3db-3aad-4eb8-ae0a-5b05f92fc7fd'
},

The current pseudo solution is (as noted below) that the problem was not the parenthesis but rather an apostrophe which was placed in the database event title as a ' mark. This needed to be eliminated. The line added to the code to accomplish this uses replace() see here:

SET @Name1 = Field(@row, "Name")
SET @Name =Replace(@Name1,"'"," i")

What is being done is to replace ' with []i so the word "what's" will now appear as what is. Tried replacing with ’ (curly apostrophe) which would be preferable but now what appears injt he fullcalendar,js element is what’ instead of what's. Because of usage though I know this needs more improvement to be fully resolved but at least the calendar displays.

Best Answer

The issue is not because of the parenthesis, its because you are closing the string too early with an apostrophe in the title of your event.

title  : 'What's new in Software (SELECTdetails 4)',

The quick fix is just to escape the apostrophe's in the string when you set the title variable:

SET @Name = Replace(Field(@row, "Name"),"'","\'")

You probably also want to encode the URL because it's likely going to create an error as well:

SET @URL = URLEncode(Field(@row, "URL"))