Correctly encode JavaScript string