Differences between javascript single quotes and double quotes

Source: Internet
Author: User

There is no difference between single quotes and double quotes.
Copy codeThe Code is as follows:
<Input type = "button" onclick = "alert (" 1 ")"> ----------------- incorrect
<Input type = "button" onclick = "alert ('1')"> ----------------- correct

Double quotation marks must be used as follows:
Var str = "abc \" def \ "ghi"
Double quotation marks cannot be parsed using a backslash.

The following is my excerpt, which is useful to you:

Write the onclick event processing code in a button on a webpage, accidentally written as follows:
Copy codeThe Code is as follows:
<Input value = "Test" type = "button" onclick = "alert (" OK ""); "/>

After an error occurs in IE, the following error occurs:
Copy codeThe Code is as follows:
<Input value = "Test" type = "button" onclick = "alert (\" OK \ ");"/>

The result is still incorrect.
At this time, I cannot figure it out. Although I know that the most direct solution is to write it like this:
Copy codeThe Code is as follows:
<Input value = "" type = "button" onclick = "alert ('OK');"/>

But why is the Escape Character \ In javascript ineffective?

Then I found a piece of normal code:
Copy codeThe Code is as follows:
<Input value = "Test" type = "button" onclick = "alert (" OK ");"/>

At this time, we understood that at this time, it still belongs to the jurisdiction of HTML, so escape characters should use HTML rather than javascript. The two double quotation marks are written in vbScript, while "\" is written in javascript, while "and" can also be used in HTML :",'.
Various expressions are listed below:
Copy codeThe Code is as follows:
<Html>
<Body>
<Input value = "Double quotation marks in double quotation marks-error" type = "button" onclick = "alert (" OK ");"/> <br/>
<Input value = "single quotes in single quotes-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 = "Double quotation marks-OK" type = "button" onclick = "alert ('OK');"/> <br/>
<Input value = "Double quotation marks in single quotes-OK" type = "button" onclick = 'alert ("OK"); '/> <br/>
<Input value = "external 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>
</Html>

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.