我用楼上的,我在字符串外面加个encodeURI没用的还是输入的内容显示出来

解决方案 »

  1.   

    function URLEncode(strURL)
    {
    var strSpecialUrl = " <>\"#%{}|^~[]`'&?+";
    var strEncode="";
    var i, j, chUrl, iCode, iCodeBin, num;
    var tempBin;
    var leadingzeros; strURL+="";
    for (i=0; i<strURL.length; i++) {
    chUrl = strURL.charAt(i);
    iCode = chUrl.charCodeAt(0);
    if (iCode<=parseInt("0x7F")) {
    if (strSpecialUrl.indexOf(chUrl)!=-1) {
    //chUrl is a special character that needs to be Url encoded
    strEncode+="%"+iCode.toString(16).toUpperCase();
    } else {
    //otherwise chrUrl is normal
    strEncode+=chUrl;
    }
    } else {
    leadingzeros="";
    iCodeBin=iCode.toString(2)
    if (iCode<=parseInt(0x7FF)) {
    //glyph is represented by two chars //check leading zeros on iCodeBin (should be 11 digits)
    for (j=11; j>iCodeBin.length; j--) leadingzeros+="0";
    iCodeBin=leadingzeros+iCodeBin tempBin="110"+iCodeBin.substr(0,5);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(5,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    } else {
    if (iCode<=parseInt(0xFFFF)) {
    //glyph is represented by three chars //check leading zeros on iCodeBin (should be 16 digits)
    for (j=16; j>iCodeBin.length; j--) leadingzeros+="0";
    iCodeBin=leadingzeros+iCodeBin tempBin="1110"+iCodeBin.substr(0,4);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(4,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(10,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    } else {
    if (iCode<=parseInt(0x1FFFFF)) {
    //glyph is represented by four chars //check leading zeros on iCodeBin (should be 21 digits)
    for (j=21; j>iCodeBin.length; j--) leadingzeros+="0";
    iCodeBin=leadingzeros+iCodeBin tempBin="11110"+iCodeBin.substr(0,3);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(3,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(9,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(15,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    } else {
    if (iCode<=parseInt(0x3FFFFFF)) {
    //glyph is represented by five chars //check leading zeros on iCodeBin (should be 26 digits)
    for (j=26; j>iCodeBin.length; j--) leadingzeros+="0";
    iCodeBin=leadingzeros+iCodeBin tempBin="111110"+iCodeBin.substr(0,2);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(2,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(8,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(14,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(20,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    } else {
    if (iCode<=parseInt(0x7FFFFFFF)) {
    //glyph is represented by six chars //check leading zeros on iCodeBin (should be 31 digits)
    for (j=31; j>iCodeBin.length; j--) leadingzeros+="0";
    iCodeBin=leadingzeros+iCodeBin tempBin="1111110"+iCodeBin.substr(0,1);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(1,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(7,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(13,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(19,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    tempBin="10"+iCodeBin.substr(25,6);
    strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
    }
    }
    }
    }
    }
    }
    }
    return strEncode;
    }
      

  2.   

    里面的字母和一些符号不会被转码
    如果想符号也转码可以用encodeURIComponent
      

  3.   

    标准的用法是 escape()不过,escape() 将汉字转化成 "%uXXXX" 的格式,这个格式 Tomcat 不兼容,IIS 上正确。
    (这是 Tomcat 的问题)