Home javascript Escaping quotes in JS

Escaping quotes in JS

Author

Date

Category

Why doesn’t it work like that?

onMouseOut = "setTimeout ('alert (\" You went away from me \ ")', 1000);"

How does it work?

onMouseOut = "setTimeout ('alert (\' You went away from me \ ')', 1000);"

Answer 1, authority 100%

Because this is html markup and instead of quotes you should write & amp; quot; :

onMouseOut = "setTimeout ('alert (& amp; quot; You went away from me & amp; quot;)', 1000 ); "

But in general, this is not necessary.


The problem is in the html markup. If you try to perform the same assignment in javascript, then the first code is correct, but the second is not (the apostrophe is escaped, the apostrophe remains in the line and the result of the curve is needed \\ '). But html’s escaping rules are different and the backslash appears in the inner line without affecting the attribute value. That is why the second line is correct, and the first one prematurely closes the attribute with a quotation mark.

PS: and the names of attribute-events are written in all small letters according to the rules.


function test1 () {
 eval ("console.log ('\' text \ '')"); // This is the text from the correct version - this does not work here
}
function test2 () {
 eval ("console.log (\" 'text' \ ")"); // Escapes characters in the outer string, not escapes in the inner
}
function test3 () {
eval ("console.log ('\\' text \\ '')"); // To escape the apostrophe in the inner line, you need to escape the backslash itself in the outer
} 
button {
 margin-bottom: .25em;
} 
& lt; button onclick = "console.log (" 'text' ")" & gt; Wrong & lt; / button & gt;
& lt; button onclick = 'console.log ("' text '")' & gt; Also wrong & lt; / button & gt;
& lt; br & gt;
& lt; button onclick = "console.log (& amp; quot; 'text' & amp; quot;)" & gt; Correct & lt; / button & gt;
& lt; button onclick = 'console.log ("& amp; # 39; text & amp; # 39;")' & gt; Also correct & lt; / button & gt;
& lt; button onclick = "console.log (& amp; quot; & amp; # 39; text & amp; # 39; & amp; quot;)" & gt; That's right & lt; / button & gt;
& lt; br & gt;
& lt; button onclick = "console.log ('\' text \ '')" & gt; Correct with escaping & lt; / button & gt;
& lt; br & gt; & lt; br & gt;
& lt; button onclick = "test1 ()" & gt; Wrong in js & lt; / button & gt;
& lt; br & gt;
& lt; button onclick = "test2 ()" & gt; Escaping external in js & lt; / button & gt;
& lt; button onclick = "test3 ()" & gt; Escaping internals in js & lt; / button & gt;
& lt; br & gt; & lt; br & gt;
& lt; button onclick = console.log ("'text'") & gt; And finally - this is how you can & lt; / button & gt; 

Answer 2

Because escaping quotes do not have to be like variable quotes. That is, if var variable = "" , then there must be ‘text’ inside for escaping ‘text’ to be performed (and vice versa).
In your case, this code will be:

onMouseOut = 'setTimeout ("alert (\" You went away from me \ ")", 1000);'

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions