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 "\"