<html>
<body>
<input type=button id="btn1" value="测试encodeString(s)" οnclick="test();"/>
<input type=text id ="user1"value="" autocomplete="on"/>
<input type=button id="btn2" value="测试alert(s)" οnclick="test2();"/>
<input type=button id="btn3" value="测试字符串长度alert(len)" οnclick="test3();"/>
<script>
function test(){
var usrval=document.getElementById("user1").value;
var usrval2=encodeString(usrval);
alert(usrval2);
//alert("dddd");
}
function test2(){
var usrval=document.getElementById("user1").value;
var usrval2=encodeString(usrval);
alert(usrval);
//alert("dddd");
}
function test3(){
var usrval=document.getElementById("user1").value;
var len=usrval.length;
//alert(usrval);
alert(len);
}
function encodeString(s){
var m = {
"\b": '\\b',
"\t": '\\t',
"\n": '\\n',
"\f": '\\f',
"\r": '\\r',
'"' : '\\"',
"\\": '\\\\'
};
if (/["\\\x00-\x1f]/.test(s)) {
return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
var c = m[b];
if(c){
return c;
}
c = b.charCodeAt();
return "\\u00" +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"';
}
return '"' + s + '"';
}
</script>
</body>
</html>