在JavaScript中通過URL傳遞漢字的方法
更新時(shí)間:2007年04月09日 00:00:00 作者:
利用JavaScript通過URL方式向后臺代碼傳值是一種經(jīng)常用到的手段,但在傳遞漢字時(shí)經(jīng)常會出現(xiàn)字符不全或變成亂碼的問題,其原因是由于客戶端IE瀏覽器的編碼方式為GB2312(簡體中文版WINDOWS的默認(rèn)設(shè)置),而后臺的C#代碼使用utf8編碼(創(chuàng)建WEB工程的默認(rèn)配置)。
網(wǎng)上有很多方案解決該問題,如將web.config的編碼方式改為GB2312、在客戶端通過escape先編碼再傳,個(gè)心體會都不是很理想或有些特殊字符不支持。經(jīng)過比較我決定使用encodeURIComponent在客戶端進(jìn)行編碼,再傳值,除了“/”不支持(但實(shí)際開發(fā)中很少需要傳遞該值,如果真有此請況,再加一層判斷即可。
encodeURIComponent的幫助文檔如下:
encodeURIComponent 方法
將文本字符串編碼為一個(gè)統(tǒng)一資源標(biāo)識符 (URI) 的一個(gè)有效組件。
encodeURIComponent( encodedURIString )
必選的 encodedURIString 參數(shù)代表一個(gè)已編碼的 URI 組件。
說明
encodeURIComponent 方法返回一個(gè)已編碼的 URI。如果您將編碼結(jié)果傳遞給 decodeURIComponent ,那么將返回初始的字符串。因?yàn)閑ncodeURIComponent 方法對所有的字符編碼,請注意,如果該字符串代表一個(gè)路徑,例如 /folder1/folder2/default.html ,其中的斜杠也將被編碼。這樣一來,當(dāng)該編碼結(jié)果被作為請求發(fā)送到 web 服務(wù)器時(shí)將是無效的。如果字符串中包含不止一個(gè) URI 組件,請使用 encodeURI 方法進(jìn)行編碼。
要求
版本 5.5
請參閱
decodeURI 方法 | decodeURIComponent 方法
應(yīng)用于: Global 對象
我做了一個(gè)小例子來展現(xiàn)該效果
Default.aspx代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標(biāo)題頁</title>
</head>
<script type="text/javascript" language="javascript">
function callURL(Value1, Value2)
{
document.URL = "Default.aspx?Value1=" + encodeURIComponent(Value1) + "&Value2=" + encodeURIComponent(Value2);
}
</script>
<body>
<form id="form1" runat="server">
<div>
Value1=<input id="Text1" type="text" value="1234567890"/><br />
Value2=<input id="Text2" type="text" value="中華人民共和國"/>
<br />
<input id="Button1" type="button" value="提交" onclick="callURL(Text1.value, Text2.value)"/></div>
</form>
</body>
</html>
Default.aspx.cs代碼:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string tmpValue1 = "";
string tmpValue2 = "";
if (Request.QueryString["Value1"] != null)
{
tmpValue1 = Request.QueryString["Value1"].ToString();
}
if (Request.QueryString["Value2"] != null)
{
tmpValue2 = Request.QueryString["Value2"].ToString();
}
Response.Write("Value1=" + tmpValue1 + "<br />" + "Value2=" + tmpValue2);
}
}
網(wǎng)上有很多方案解決該問題,如將web.config的編碼方式改為GB2312、在客戶端通過escape先編碼再傳,個(gè)心體會都不是很理想或有些特殊字符不支持。經(jīng)過比較我決定使用encodeURIComponent在客戶端進(jìn)行編碼,再傳值,除了“/”不支持(但實(shí)際開發(fā)中很少需要傳遞該值,如果真有此請況,再加一層判斷即可。
encodeURIComponent的幫助文檔如下:
encodeURIComponent 方法
將文本字符串編碼為一個(gè)統(tǒng)一資源標(biāo)識符 (URI) 的一個(gè)有效組件。
encodeURIComponent( encodedURIString )
必選的 encodedURIString 參數(shù)代表一個(gè)已編碼的 URI 組件。
說明
encodeURIComponent 方法返回一個(gè)已編碼的 URI。如果您將編碼結(jié)果傳遞給 decodeURIComponent ,那么將返回初始的字符串。因?yàn)閑ncodeURIComponent 方法對所有的字符編碼,請注意,如果該字符串代表一個(gè)路徑,例如 /folder1/folder2/default.html ,其中的斜杠也將被編碼。這樣一來,當(dāng)該編碼結(jié)果被作為請求發(fā)送到 web 服務(wù)器時(shí)將是無效的。如果字符串中包含不止一個(gè) URI 組件,請使用 encodeURI 方法進(jìn)行編碼。
要求
版本 5.5
請參閱
decodeURI 方法 | decodeURIComponent 方法
應(yīng)用于: Global 對象
我做了一個(gè)小例子來展現(xiàn)該效果
Default.aspx代碼:
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標(biāo)題頁</title>
</head>
<script type="text/javascript" language="javascript">
function callURL(Value1, Value2)
{
document.URL = "Default.aspx?Value1=" + encodeURIComponent(Value1) + "&Value2=" + encodeURIComponent(Value2);
}
</script>
<body>
<form id="form1" runat="server">
<div>
Value1=<input id="Text1" type="text" value="1234567890"/><br />
Value2=<input id="Text2" type="text" value="中華人民共和國"/>
<br />
<input id="Button1" type="button" value="提交" onclick="callURL(Text1.value, Text2.value)"/></div>
</form>
</body>
</html>
Default.aspx.cs代碼:
復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string tmpValue1 = "";
string tmpValue2 = "";
if (Request.QueryString["Value1"] != null)
{
tmpValue1 = Request.QueryString["Value1"].ToString();
}
if (Request.QueryString["Value2"] != null)
{
tmpValue2 = Request.QueryString["Value2"].ToString();
}
Response.Write("Value1=" + tmpValue1 + "<br />" + "Value2=" + tmpValue2);
}
}
您可能感興趣的文章:
- javascript實(shí)現(xiàn)漢字轉(zhuǎn)拼音代碼分享
- javascript限制用戶只能輸漢字中文的方法
- javascript正則匹配漢字、數(shù)字、字母、下劃線
- javascript下漢字和Unicode編碼互轉(zhuǎn)代碼
- javascript 漢字轉(zhuǎn)拼音實(shí)現(xiàn)代碼
- Javascript 漢字字節(jié)判斷
- JavaScript 給漢字排序?qū)嵗a
- 用javascript實(shí)現(xiàn)的漢字簡繁轉(zhuǎn)換
- javascript 漢字與拼音轉(zhuǎn)換
- javascript漢字轉(zhuǎn)拼音的代碼
- JavaScript實(shí)現(xiàn)ASC轉(zhuǎn)漢字及漢字轉(zhuǎn)ASC的方法
相關(guān)文章
純js和css實(shí)現(xiàn)漸變色包括靜態(tài)漸變和動(dòng)態(tài)漸變
用javascript實(shí)現(xiàn)一下所謂的動(dòng)態(tài)漸變,考慮動(dòng)態(tài)原因就不上圖了,我來簡單介紹下思路2014-05-05
微信小程序?qū)崿F(xiàn)自動(dòng)回復(fù)圖片消息
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)客服消息自動(dòng)回復(fù)圖片消息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
JavaScript對象解構(gòu)的用法實(shí)例解析
解構(gòu)賦值允許你使用類似數(shù)組或?qū)ο笞置媪康恼Z法將數(shù)組和對象的屬性賦給各種變量,下面這篇文章主要給大家介紹了關(guān)于JavaScript對象解構(gòu)用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
js調(diào)用webservice中的方法實(shí)現(xiàn)思路及代碼
js調(diào)用webservice還算是一個(gè)比較實(shí)用的功能,本文提供了實(shí)現(xiàn)思路及代碼,感興趣的你可不要錯(cuò)過了哈,希望本文可以幫助到你啊2013-02-02
淺談javascript中for in 和 for each in的區(qū)別
兩個(gè)的作用都用來遍歷對象,但為什么有了for in語句了還要for each in語句呢,后來看了下for each in開發(fā)的文檔,for each in是作為E4X標(biāo)準(zhǔn)的一部分在javascript 1.6中發(fā)布的,而且它不是ECMAScript標(biāo)準(zhǔn)的一部分2015-04-04
doctype后如何獲得body.clientHeight的方法
doctype后如何獲得body.clientHeight的方法...2007-07-07

