How should I escape a string that will be going into a Javascript String? URLEncode(X)? str_replace(\"\'\",\"\\\'\",X)?
Use json_encode if available (since PHP 5.2):
var str = ;
Otherwise use you can use rawurlencode and decode it with decodeURIComponent:
var str = decodeURIComponent("");