ManiSto
ManiSto

Reputation: 658

JavaScript alert with escaped characters fail

Why does the following JavaScript fail, when you click on the link? And is there any way to fix it?

<a onclick="alert('Do you want to delete &lt; &gt; &quot; &#39;?');">Link</a>

I am aware that the escaped characters would be illegal if they were printed literally, but I can't see why it should fail when then are escaped. Leaving out the characters is not an option, as it is user defined.

Upvotes: 0

Views: 4120

Answers (1)

Arbel
Arbel

Reputation: 30989

&#39; is considered a single quote, you need to escape it with a \, so \&#39;

Without escaping, javascript will throw an "unterminated string literal" error.

This works:

<a onclick="alert('Do you want to delete &lt; &gt; &quot; \&#39;?');">Link</a>

Upvotes: 2

Related Questions