Passing text to a javascript function that can have one quote

I have a link that I dynamically create that looks something like this:

<a onclick="Edit('value from a text column here')" href="javascript:void(null);">Edit</a>

using the "Edit" function, then passing the passed value and placing it in the Rich Text text editor. This works well, unless there is one quote in the text to be transmitted. The obvious problem is that the link then looks something like this:

<a onclick="Edit('I'm a jelly donut')" href="javascript:void(null);">Edit</a>

Any suggestions on what I can do? I would prefer not to go too far from the structure that I am currently using, because it is a bit of a standard (and maybe standard sucks, but this is another question at all).

Note. I use ASP as my server language.

+3
source share
5

HTML, &quot; .., HTML. wikipedia HTML/XML.

, , , : PHP htmlspecialchars, ASP .., , .

+14

'with \'

+3

escape-, a\before. , , , .

client-side javascript ( , document.createElement), String.replace , , .

+1

:

JavaScript

<a onclick="Edit('I\u0027m a jelly donut')" href="javascript:void(null);">Edit</a>

as\u0027 espace javascript . :

JavaScript

<script>function getQuote() { return "'" }</script>
<a onclick="Edit('I' + getQuote() + 'm a jelly donut')" href="javascript:void(null);">Edit</a>

JavaScript

<script>var g_strJellyText = "I\u0027m a jelly donut"</script>
<a onclick="Edit(g_strJellyText)" href="javascript:void(null);">Edit</a>

, ( , HTML, ).

, .

+1

, , , , , , , .

, Rich Text Editor, YUI datatable, , .

"" "" , , datatable .

The problem with using the htmlspecialchars ASP equivalent (Server.HTMLEncode) was that it still had a problem in javascript that was generated from a custom formatting function. I don’t know why, but I tried and it didn’t work. This answer was closest to the answer I came up with (and pushed me in the right direction), so I accepted this answer.

Instead, I used the javascript "escape" function to go into my editing function, and then unescape to set the internal HTML for the Rich Text Editor.

0
source

Source: https://habr.com/ru/post/1697730/


All Articles