[SalesForce] Long Text Area field in javascript custom button

I have a custom button (JavaScript) which copies the Description field from Event to my custom Object like this:

summary.Discussion_Topics__c = '{!Event.Description}';

Both fields are of Long Text Area(32000) type.
The button works fine when the Description has one line. When it includes multiple lines the javascript throws an exception. I would like to retain the formatting and copy the Description with multiple line.
Can that be achieved? How?

Best Answer

The JSENCODE Visualforce function will escape the line feeds and other unsafe characters:

summary.Discussion_Topics__c = '{!JSENCODE(Event.Description)}';

This works in custom button JavaScript too e.g. in a custom "Execute JavaScript" button on Account:

var d = '{!JSENCODE(Account.Description)}';
alert(d);
Related Topic