The simple code block below can be served up in a static HTML page but results in a JavaScript error. How should you escape the embedded double quote in the onClick
handler (i.e. “xyz)? Note that the HTML is generated dynamically by pulling data from a database, the data of which is snippets of other HTML code that could have either single or double quotes. It seems that adding a single backslash ahead of the double quote character doesn’t do the trick.
JavaScript
x
8
1
<script type="text/javascript">
2
function parse(a, b, c) {
3
alert(c);
4
}
5
</script>
6
7
<a href="#x" onclick="parse('#', false, '<a href="xyz'); return false">Test</a>
8
Advertisement
Answer
Did you try
"
or x22
instead of
JavaScript
1
2
1
"
2
?