how to escape double quotes

I have some javascript code that I'm using within Salesforce (CRM system).  The javascript is invoked on a button click.  What is happening is if the description field that is passed in thru the URL contains double quotes, it throws an error showing that a semi-colon is expected.

How can I escape the double qoutes if they are included in the description so the error does not occur?

this is the line for the description getting passed in:

00N70000002MF8u={!Service_Booking__c.Description__c}

So, if that value contains double quotes, the error is thrown.  I just need to escape the double quotes.

Thanks for any help.
{! requireScript("/soap/ajax/12.0/connection.js") } 
function doit(){ 
var ul="&CF00N70000002MF8y={!Service_Booking__c.Name}&00N70000002MF90={!Service_Booking__c.Start__c}&00N70000002MF8v={!Service_Booking__c.End__c}&00N70000002MF8t={!Service_Booking__c.Delivery_Type__c}&00N70000002MF8s={!Service_Booking__c.Delivery_Sub_type__c}&CF00N70000002MF4e={!Service_Booking__c.Services_Engagement__c}&00N70000002MF8u={!Service_Booking__c.Description__c}&00N70000002MF8r={!Service_Booking__c.Billable_Days__c}&CF00N70000002MF8z={!Service_Booking__c.Site__c}"; 
 
var newURL = "{!URLFOR( $Action.Service_Delivery__c.New, Service_Delivery__c.Id,[retURL=$Request.retURL] ,true)}"+ ul; 
 
if({!Service_Booking__c.Active__c}==true){ 
window.parent.location.replace(newURL); 
} 
else if({!Service_Booking__c.Active__c}==false){ 
alert("You attempted to convert Service Booking {!Service_Booking__c.Name}, which is no longer active. This usually means the booking has already been converted to a delivery. Check the record and make sure."); 
window.parent.location.replace("/apex/ConvertSingle?id={!Service_Booking__c.Id}"); 
} 
 
 
} 
doit();

Open in new window

-Dman100-Software ConsultantAsked:
Who is Participating?
 
Samuel LiewConnect With a Mentor Commented:
Ignore the previous post. EE put extra line breaks in the long line.
<script type="text/javascript">
var regexp = new RegExp("\"", "g");
var rep    = '\"'
 
var booking_name = escape('{!Service_Booking__c.Name}');
    booking_name = booking_name.replace(regexp, rep);
 
var booking_start= escape('{!Service_Booking__c.Start__c}');
    booking_start= booking_name.replace(regexp, rep);
 
var booking_end  = escape('{!Service_Booking__c.End__c}');
    booking_end  = booking_name.replace(regexp, rep);
 
var booking_delt = escape('{!Service_Booking__c.Delivery_Type__c}');
    booking_delt = booking_name.replace(regexp, rep);
 
var booking_delst= escape('{!Service_Booking__c.Delivery_Sub_type__c}');
    booking_delst= booking_name.replace(regexp, rep);
 
var booking_se   = escape('{!Service_Booking__c.Services_Engagement__c}');
    booking_se   = booking_name.replace(regexp, rep);
 
var booking_desc = escape('{!Service_Booking__c.Description__c}');
    booking_desc = booking_name.replace(regexp, rep);
 
var booking_bd   = escape('{!Service_Booking__c.Billable_Days__c}');
    booking_bd   = booking_name.replace(regexp, rep);
 
var booking_site = escape('{!Service_Booking__c.Site__c}');
    booking_site = booking_name.replace(regexp, rep);
 
var ul= "&CF00N70000002MF8y="+booking_name+"&00N70000002MF90="+booking_start+"&00N70000002MF8v="+booking_end+"&00N70000002MF8t="+booking_delt+"&00N70000002MF8s="+booking_delst+"&CF00N70000002MF4e="+booking_se+"&00N70000002MF8u="+booking_desc+"&00N70000002MF8r="+booking_bd+"&CF00N70000002MF8z="+booking_site+""; 
 
alert("You should see double quotes below if there are any:\n\n"+ul);
</script>

Open in new window

0
 
Samuel LiewCommented:
try adding this line below to replace them
function doit(){ 
var ul="&CF00N70000002MF8y={!Service_Booking__c.Name}&00N70000002MF90={!Service_Booking__c.Start__c}&00N70000002MF8v={!Service_Booking__c.End__c}&00N70000002MF8t={!Service_Booking__c.Delivery_Type__c}&00N70000002MF8s={!Service_Booking__c.Delivery_Sub_type__c}&CF00N70000002MF4e={!Service_Booking__c.Services_Engagement__c}&00N70000002MF8u={!Service_Booking__c.Description__c}&00N70000002MF8r={!Service_Booking__c.Billable_Days__c}&CF00N70000002MF8z={!Service_Booking__c.Site__c}"; 
 
ul = ul.replace(/\"/g,'\\\"');

Open in new window

0
 
-Dman100-Software ConsultantAuthor Commented:
Hi sam2912,

I tried adding the line:
ul = ul.replace(/\"/g,'\\\"');

I tested using a description with double quotes and still the error occurred.

Any thoughts?  As soon as I remove the double quotes, everything works fine?
0
 
Samuel LiewCommented:
Try this. I can't actually test it out because the values are dynamic.
var regexp = new RegExp("\"", "g");
var rep    = '\"'
 
var booking_name = escape('{!Service_Booking__c.Name}');
    booking_name = booking_name.replace(regexp, rep);
 
var booking_start= escape('{!Service_Booking__c.Start__c}');
    booking_start= booking_name.replace(regexp, rep);
 
var booking_end  = escape('{!Service_Booking__c.End__c}');
    booking_end  = booking_name.replace(regexp, rep);
 
var booking_delt = escape('{!Service_Booking__c.Delivery_Type__c}');
    booking_delt = booking_name.replace(regexp, rep);
 
var booking_delst= escape('{!Service_Booking__c.Delivery_Sub_type__c}');
    booking_delst= booking_name.replace(regexp, rep);
 
var booking_se   = escape('{!Service_Booking__c.Services_Engagement__c}');
    booking_se   = booking_name.replace(regexp, rep);
 
var booking_desc = escape('{!Service_Booking__c.Description__c}');
    booking_desc = booking_name.replace(regexp, rep);
 
var booking_bd   = escape('{!Service_Booking__c.Billable_Days__c}');
    booking_bd   = booking_name.replace(regexp, rep);
 
var booking_site = escape('{!Service_Booking__c.Site__c}');
    booking_site = booking_name.replace(regexp, rep);
 
var ul= 
 
"&CF00N70000002MF8y="+booking_name+"&00N70000002MF90="+booking_start+"&00N70000002MF8v="+booking_end+"&00N70000002MF8t="+booking_delt+"&00N70000002MF8s="+b
 
ooking_delst+"&CF00N70000002MF4e="+booking_se+"&00N70000002MF8u="+booking_desc+"&00N70000002MF8r="+booking_bd+"&CF00N70000002MF8z="+booking_site+""; 
 
alert("You should see double quotes below if there are any:\n\n"+ul);

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.