JavaScript single and double quotes distinguish and process _ basics

Source: Internet
Author: User
Single and double quotes don't really make a difference, see you're used to it
Copy Code code as follows:

<input type= "button" onclick= "Alert (" 1 ")" >-------------------Incorrect
<input type= "button" onclick= "alert (' 1 ')" >-------------------correct

Double quotes with double quotes to do this:
var str = "abc\" def\ "Ghi"
Use a backslash to prevent the parsing of double quotes.

Here is my excerpt, I hope to be useful to you:

In a Web page The button writes the onclick event's handling code, which is accidentally written as follows:
Copy Code code as follows:

<input value= "Test" type= "button" onclick= "Alert" ("OK"); "/>

When IE prompts an error, then casually change to:
Copy Code code as follows:

<input value= "Test" type= button "onclick=" alert (\ "Ok\"); "/>

The result is still an error.
At this point, I can not figure out, although I know the most direct solution is written in this way:
Copy Code code as follows:

<input value= "type=" button "onclick=" alert (' OK '); "/>

But why does the escape character in JavaScript have no effect?

Then I found a normal code:
Copy Code code as follows:

<input value= "Test" type= "button" onclick= "alert (" OK ");"/>

It was then understood that, at this time, it was still within the jurisdiction of the HTML, so the escape character should be used in HTML instead of JavaScript. Two double quotes are VBScript, \ "This practice is JavaScript, and HTML is used, and in addition, you can use:".
Here are a list of the various expression methods:
Copy Code code as follows:

<body>
<input value= "Outside double quotation mark inside double quotation mark-Error" type= "button" onclick= "alert (" OK ");"/><br/>
<input value= "single quotation mark inside 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 quotes-Error" type= "button" onclick= "alert (\" Ok\ ");"/><br/>
<input value= "\+ single quotes-Error" type= "button" onclick= "alert (\ ' Ok\ ');"/><br/>
<input value= "outside double quotes inside single quotes-ok" type= "button" onclick= "alert (' OK ');"/><br/>
<input value= "Outside single quotation marks inside double quotes-ok" type= "button" onclick= "alert (" OK "); '/><br/>
<input value= "Outside do not use quotation marks-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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.