Javascript single and double quotes distinction and handling

  • 2020-03-30 02:57:20
  • OfStack

There's no difference between a single quote and a double quote, depending on how you get used to it
 
<input type="button" onclick="alert("1")">------------------- Is not correct  
<input type="button" onclick="alert('1')">------------------- correct  

Use double quotes again in double quotes like this:
Var STR = "ABC \" def \ "ghi"
Use backslashes to prevent parsing double quotes.

Here are some excerpts I hope you found useful:

In a web button, write the onclick event handling code, accidentally written as follows:
 
<input value="Test" type="button" onclick="alert(""OK"");" /> 

IE prompt error, then casually changed to:
 
<input value="Test" type="button" onclick="alert("OK");" /> 

The result is still wrong.
At this point, I couldn't figure it out, although I knew the most direct solution was to write it like this:
 
<input value="" type="button" onclick="alert('OK');" /> 

But why does the escape character \ not work in javascript?

Then I found a normal piece of code:
 
<input value="Test" type="button" onclick="alert("OK");" /> 

At this point, it is understood that, at this point, it is still within the jurisdiction of HTML, so escape characters should be HTML, not javascript. Two double quotes are used in vbScript, \" this is javascript, and HTML is used in ", in addition to: ", '.
Here are some ways to express it:
 
<html> 
<body> 
<input value=" Outer double quotation marks inside double quotation marks - error " type="button" onclick="alert("OK");" /><br /> 
<input value=" Outer single quotation mark and inner single quotation mark - error " type="button" onclick='alert('OK');' /><br /> 
<input value=" Two double quotes - error " type="button" onclick="alert(""OK"");" /><br /> 
<input value=" Two single quotes - error " type="button" onclick="alert(''OK'');" /><br /> 
<input value="+ Double quotation marks - error " type="button" onclick="alert("OK");" /><br /> 
<input value="+ Single quotes - error " type="button" onclick="alert('OK');" /><br /> 
<input value=" Outer double quotation marks and inner single quotation marks -OK" type="button" onclick="alert('OK');" /><br /> 
<input value=" Outer single quotation mark inside double quotation mark -OK" type="button" onclick='alert("OK");' /><br /> 
<input value=" No quotes are used externally -OK" type="button" onclick=alert('OK');alert("OK"); /><br /> 
<input value="HTML Escape character "(& # 3 4 ;)-OK" type="button" onclick="alert("OK");" /><br /> 
<input value="HTML Escape character '(& # 3 9 ;)-OK" type="button" onclick="alert('OK');" /><br /> 
<input value="HTML Escape character "(& # x 2 2 ;)-OK" type="button" onclick="alert('OK');" /><br /> 
<input value="HTML Escape character '(& # x 2 7 ;)-OK" type="button" onclick="alert('OK');" /><br /> 
<input value="HTML Escape character "(& q u o t ;)-OK" type="button" onclick="alert("OK");" /><br /> 
<input value="HTML Escape character '(& a p o s ;)-IE error " type="button" onclick="alert('OK');" /><br /> 
<input value=" other \- error " type="button" onclick="alert(\"OK\");" /><br /> 
<input value=" other & # 3 4 ;- error " type="button" onclick="alert("OK");" /><br /> 
</body> 
</html> 

Related articles: