How to escape double quotes in JavaScript's onClick event handler
P粉926174288
2023-08-22 13:12:47
<p>The simple code block below will work in a static HTML page, but will result in a JavaScript error. How should you escape the double quotes (i.e. "xyz") embedded in the <code>onClick</code> handler? Note that the HTML is dynamically generated by pulling data from the database that is the source of the other HTML code Fragment, may contain single or double quotes. It seems that adding a backslash before the double quote character does not solve the problem.</p>
<pre class="brush:php;toolbar:false;"><script type="text/javascript">
function parse(a, b, c) {
alert(c);
}
</script>
<a href="#x" onclick="parse('#', false, '<a href="xyz'); return false">Test</a></pre>
It requires HTML escaping, not Javascript escaping. Change
\"
to"
Have you tried
"
or\x22
instead of
?