This is an example of string that should I output in a javascript alert();
string with "double quote" in it
Because this string can be edited via PHP by my users it's better to prevent XSS attacks. To do so in the HTML of my document I usually do:
<?php echo( htmlspecialchars( $MY_STRING, ENT_QUOTES, 'UTF-8' ) ); ?>
That works great.
But now I just noticed that if I output the same string in a javascript alert:
<script>
alert( "<?php echo( htmlspecialchars( $MY_STRING, ENT_QUOTES, 'UTF-8' ) ); ?>" );
</script>
The output of the alert in this case is:
string with "double quote" in it
What is the best way to output the double quotes in a alert, but also preventig XSS injection?