JS Escape Character

Source: Internet
Author: User

< Div ID = "Divc"   />
< Script Language = "JavaScript" >
VaR Div = Document. getelementbyid ( " Divc " );
VaR Html =   ""
Html + =   ""
+   " <H1> "
+   " <A href = 'javascript:; 'onclick = \ " Alert ( ' Javascript ' )\ " > DHTML innerhtml propery. </a> "
+   " </H1> " ;
Div. innerhtml = HTML;
</ Script >

If you get used to writing it, it is not troublesome. But is there a simpler method? See the following example:

< Script Language = "JavaScript" >
VaR Html = ' \
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0"> \
<Tr> \
<TD> & nbsp; </TD> \
</Tr> \
<Tr> \
<TD> & nbsp; </TD> \
</Tr> \
</Table> \
' ;
Alert (HTML );
</ Script >
Isn't it so troublesome? But pay attention to the following example.
< Script Language = "JavaScript" >
// Escape single quotes \'
VaR Html = ' \
<H1> \
Javascript skills \
</H1> \
<A href = "javascript:;" onclick = "alert (\ ' Javascript \ ' ) "> JavaScript escape </a> </font> \
<Br/> \
Power \ ' Bluedestiny, never - Online \ ' \
' ;
Alert (HTML );
</ Script >
"\" Must be used for the escape "\"

'-------------------------------------------------------
'Principle:
'-------------------------------------------------------
This is my personal opinion. If there is something wrong, please point out:
Let's take a look at the example:
< Script Language = "JavaScript" >
// There is a space before characters a of S1 and S2.
S1 = ' \
A ' ;
S2 = ' A ' ;
Document. Write ( " S1: "   + S1.length +   " \ NS2: "   + S2.length );
</ Script >
Output result:
S1: 2 S2: 2
That is to say, the escape character escapes the carriage return! That is to say
Let's look at another example:
< Script Language = "JavaScript" >
// The following string contains spaces, that is, S1 =.
S1 = ' \
A ' ;
Document. Write ( " S1: "   + S1.length );
</ Script >
Output Error. error message: the String constant is not ended.
That is to say, it is because a space is added. Try again.
< Script Language = "JavaScript" >
S1 = ' \\
A ' ;
Document. Write ( " S1: "   + S1.length );
</ Script >
The result is obvious. In the string, the "\" Escape Character can escape the carriage return (that is, the carriage return character does not exist), but the Tab character cannot be used, and space characters escape (they exist, as described in the above example ).

Finally, let's give you a small tips. Do you still remember the Code above?
script language = "JavaScript" >
// escape single quotes with \ '
var HTML = ' \< br>

\< br> JavaScript skills \

\< br> JavaScript escape \

\
power by \ ' bluedestiny, never - online \ ' \< br> ' ;
alert (HTML );
script

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.