How to escape double quotes in JavaScript's onClick event handler
P粉926174288
P粉926174288 2023-08-22 13:12:47
0
2
527
<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>
P粉926174288
P粉926174288

reply all(2)
P粉709644700

It requires HTML escaping, not Javascript escaping. Change \" to "

P粉402806175

Have you tried

" or \x22

instead of

\"

?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template