With the use of JavaScript, I am able to generate HTML code dynamically. For instance, I can add a function that triggers when a link is clicked, like so:
$('#myDiv').append('<a href="javascript:start(\''+TERM+'\');">click</a>');
Therefore, start()
will be executed upon clicking the link. The variable TERM
could contain a single word such as world
or moody's
. In this case, the generated HTML code would appear as follows:
<a href="javascript:start('world');">click</a>
OR
<a href="javascript:start('moody's');">click</a>
As illustrated above, the second example will not function correctly. Therefore, I have decided to "escape" the TERM
, like this:
$('#myDiv').append('<a href="javascript:start(\''+escape(TERM)+'\');">click</a>');
Upon inspecting the HTML source-code using Firebug, it is evident that the following code was produced:
<a href="javascript:start('moody%27s');">click</a>
This solution works well until the link is actually clicked - at which point the browser (in this case Firefox) seems to interpret the %27
and attempts to execute start('moody's');
Is there a way to persistently escape the term without interpreting the %27
before the term is processed in JS? Are there alternative solutions besides utilizing regular expressions to convert '
to \'
?