JS technique--the magical meaning of escape character "\"

Source: Internet
Author: User

JS technique--the magical meaning of escape character "\"

Bluedestiny, Never-online//Bluedestiny [at] 126.com

Normally, when we are dynamically given a container innerhtml, we usually do the following things:
<div id= "DIVC"/>
<script language= "JavaScript" >
var div = document.getElementById ("DIVC");
var html = ""
HTML + = ""
+ "+ "<a href= ' javascript:; ' onclick=\" alert (' JavaScript ') \ ">dhtml InnerHTML propery.</a>"
+ "div.innerhtml = html;
</SCRIPT>

It is not troublesome to write a habit, but is there a simpler way? Take a look at the following example:

<script language= "JavaScript" >
var html= ' \
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" >\
<tr>\
<td> </td>\
</tr>\
<tr>\
<td> </td>\
</tr>\
</table>\
‘;
alert (HTML);
</SCRIPT>
Is it not so much trouble? But there are a few points to note, see the following example
<script language= "JavaScript" >
To escape single quotes with \ '
var html= ' \
JavaScript tips \
<a href= "javascript:;" onclick= "alert (\ ' javascript\ ')" >javascript escape </a></font>\
<br/>\
Power by \ ' Bluedestiny, never-online\ ' \
‘;
alert (HTML);
</SCRIPT>
The place to escape is still a "\"

‘-------------------------------------------------------
Principle
‘-------------------------------------------------------
This is my own personal opinion, if there is a wrong place, please point out:
Or look at an example:
<script language= "JavaScript" >
S1 and S2 have a space before the character a
S1= ' \
A ';
S2= ' a ';
document.write ("S1:" + s1.length + "\ns2:" + s2.length);
</SCRIPT>
Output Result:
S1:2 S2:2
That means the escape character escapes the carriage return! Other words
Let's look at an example:
<script language= "JavaScript" >
The following string is a space, that is, the s1= ' \.
S1= ' \
A ';
document.write ("S1:" + s1.length);
</SCRIPT>
Output error, error message: string constant not ended.
That is, the reason for the extra space. Then try it 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 does not exist), but not the tabs, and the space character escapes (they are present, the above example illustrates this).

Finally, give everyone a little tips, remember the above code?
<script language= "JavaScript" >
To escape single quotes with \ '
var html= ' \
JavaScript tips \
<a href= "javascript:;" onclick= "alert (\ ' javascript\ ')" >javascript escape </a></font>\
<br/>\
Power by \ ' Bluedestiny, never-online\ ' \
‘;
alert (HTML);
</SCRIPT>
Take a closer look at the modal frame you're bouncing, and see what the string looks like? It should be understood.

JS technique--the magical meaning of escape character "\"

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.