javascript 錯誤“缺少十六進位數字”的處理

來源:互聯網
上載者:User
問題:

我是打算使用上面的語句把資料庫的連接字串寫入上一級網頁的connstr文字框中,但是在執行時ie總報告“缺少十六進位數字”。

代碼如下:
a="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:/網站設計/dianming/upload/student.mdb;"
Response.Write("<script>")
    html="parent.document.getElementById(""connstr"").value="""+a+""""
    Response.Write(html+";")
Response.Write("</script>")

問題分析:

到底是怎麼回事呢?經過實驗發現,問題出在字串a中的 “/” 上,    原來javascript中“/”是個特殊的字元,在很多場合需要轉換,為此我使用“/“臨時替代”/“一下,在使用到此字串的時候再轉換回來就可以了。
代碼改寫如下:

a="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:/網站設計/dianming/upload/student.mdb;"
html=html+replace(a,"/","/")+""""

Response.Write("<script>")
    html="parent.document.getElementById(""connstr"").value="""+a+""""
    Response.Write(html+";")
Response.Write("</script>")

參考:
例子程式:

*Encode for HTML.
  */
  public static String htmlEncoder(String str)
  {
    if(str==null || str.equals(""))
      return "";
    String res_str;
    res_str=strReplace(str,"&","&amp;");
    res_str=strReplace(str," ","&nbsp;");
    res_str=strReplace(str,"<","&lt;");
    res_str=strReplace(str,">","&rt;");
    res_str=strReplace(str,"/"","&quot;");
    res_str=strReplace(str,"'","&#039;");
    return res_str;
  }

  /**
  *Encode for HTML-Text.
  */
  public static String htmlTextEncoder(String str)
  {
    if(str==null || str.equals(""))
      return "";
    String res_str;
    res_str=strReplace(str,"&","&amp;");
    res_str=strReplace(str,"<","&lt;");
    res_str=strReplace(str,">","&rt;");
    res_str=strReplace(str,"/"","&quot;");
    res_str=strReplace(str,"'","&#039;");
    res_str=strReplace(str," ","&nbsp;");
    res_str=strReplace(str,"/r/n","<br/>");
    res_str=strReplace(str,"/r","<br/>");
    res_str=strReplace(str,"/n","<br/>");
    return res_str;
  }

  /**
  *Encode for URL.
  */
  public static String urlEncoder(String str) {
    return java.net.URLEncoder.encode(str) ;
  }

  /**
  *Encode for XML.
  */
  public static String xmlEncoder(String str)
  {
    if(str==null || str.equals(""))
      return "";
    String res_str;
    res_str=strReplace(str,"&","&amp;");
    res_str=strReplace(res_str,"<","&lt;");
    res_str=strReplace(res_str,">","&gt;");
    res_str=strReplace(res_str,"/"", "&quot;");
    res_str=strReplace(res_str,"/'", "&acute;");
    return res_str;
  }

  /**
  *Encode for SQL.
  */
  public static String sqlEncoder(String str)
  {
    if(str==null || str.equals(""))
      return "";
    String res_str;
    res_str=strReplace(str,"'","''");
    return res_str;
  }

  /**
  *Encode for Javascript.
  */
  public static String jsEncoder(String str)
  {
    if(str==null || str.equals(""))
      return "";
    String res_str;
    res_str=strReplace(str,"'","//'");
    res_str=strReplace(str,"/"","///"");
    res_str=strReplace(str,"/r/n","///n");
    res_str=strReplace(str,"/n","///n");
    res_str=strReplace(str,"/r","///n");
    return res_str;
  }

html=html+replace(table_list(fileExt,path,2),"/","/")+"""" 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.