I have a php page on which I show a jQuery alert when a user adds products to their cart. The message displayed in the alert is a variable from a php page where all language elements are stored.
$cartaddeditem='The product was added to your cart.\r\n You can view your <a href=\'cart.php?action=show\'>shopping cart</a> or click OK to continue shopping';
Then, on the page:
<body onload="jAlert('success', '<?=$cartaddeditem?>', '<?=$minicart?>');" <? } >
The problem is that the alert fails to fire because of the quotes in the $cartaddeditem var and I’m getting an error in firebug that says:
Error: missing ) after argument list
Line: 1, Column: 22
Source Code:
jAlert('success', The product was added to your cart.\r\n You can view your <a href=\
How do I escape the quote in the message string for jQuery? I tried several escaping types with double and single quotes, one or two backslashes but nothing worked.
Try using double quotes to quote your HTML attributes.
May I also suggest not adding
onloadattributes and using$(document).ready(instead?UPDATE:
You can also use
json_encodeto make sure that quotes don’t clash.json_encodeworks on strings too, it outputs a a string surrounded by double quotes (so don’t add quotes around the parameter).