Escaping JavaScript strings

From: Date: Thu, 29 May 2008 10:40:08 +0000
Subject: Escaping JavaScript strings
Groups: php.general 
Request: Send a blank email to php-general+get-274802@lists.php.net to get a copy of this message
Hello, I'm adding functionality to allow a user to copy data on a page to the clipboard using JS. This can be done simply with: window.clipboardData.setData('Text','String to copy to clipboard'); The string from PHP needs to contain line breaks when copied into the clipboard. This works when I use \n, e.g. 'This string\nspans multiple\nlines' but how do I escape this string when outputting it from PHP? addslashes() seems the obvious choice, but as it escapes backslashes, the \n chars end up in the clipboard! Using hard new lines in the argument passed to the JS function doesn't work as this causes JS errors: DOESN'T WORK: <script type="text/javascript"> var sCopy = 'String to copy to clipboard'; </script> <input type="button" value="COPY" onclick="window.clipboardData.setData('Text',sCopy);" /> Any ideas? Thanks, Edward

« previous php.general (#274802) next »