在JavaScript中通過(guò)URL傳遞漢字的方法
更新時(shí)間:2007年04月09日 00:00:00 作者:
利用JavaScript通過(guò)URL方式向后臺(tái)代碼傳值是一種經(jīng)常用到的手段,但在傳遞漢字時(shí)經(jīng)常會(huì)出現(xiàn)字符不全或變成亂碼的問(wèn)題,其原因是由于客戶端IE瀏覽器的編碼方式為GB2312(簡(jiǎn)體中文版WINDOWS的默認(rèn)設(shè)置),而后臺(tái)的C#代碼使用utf8編碼(創(chuàng)建WEB工程的默認(rèn)配置)。
網(wǎng)上有很多方案解決該問(wèn)題,如將web.config的編碼方式改為GB2312、在客戶端通過(guò)escape先編碼再傳,個(gè)心體會(huì)都不是很理想或有些特殊字符不支持。經(jīng)過(guò)比較我決定使用encodeURIComponent在客戶端進(jìn)行編碼,再傳值,除了“/”不支持(但實(shí)際開(kāi)發(fā)中很少需要傳遞該值,如果真有此請(qǐng)況,再加一層判斷即可。
encodeURIComponent的幫助文檔如下:
encodeURIComponent 方法
將文本字符串編碼為一個(gè)統(tǒng)一資源標(biāo)識(shí)符 (URI) 的一個(gè)有效組件。
encodeURIComponent( encodedURIString )
必選的 encodedURIString 參數(shù)代表一個(gè)已編碼的 URI 組件。
說(shuō)明
encodeURIComponent 方法返回一個(gè)已編碼的 URI。如果您將編碼結(jié)果傳遞給 decodeURIComponent ,那么將返回初始的字符串。因?yàn)閑ncodeURIComponent 方法對(duì)所有的字符編碼,請(qǐng)注意,如果該字符串代表一個(gè)路徑,例如 /folder1/folder2/default.html ,其中的斜杠也將被編碼。這樣一來(lái),當(dāng)該編碼結(jié)果被作為請(qǐng)求發(fā)送到 web 服務(wù)器時(shí)將是無(wú)效的。如果字符串中包含不止一個(gè) URI 組件,請(qǐng)使用 encodeURI 方法進(jìn)行編碼。
要求
版本 5.5
請(qǐng)參閱
decodeURI 方法 | decodeURIComponent 方法
應(yīng)用于: Global 對(duì)象
我做了一個(gè)小例子來(lái)展現(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>無(wú)標(biāo)題頁(yè)</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="中華人民共和國(guó)"/>
<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)上有很多方案解決該問(wèn)題,如將web.config的編碼方式改為GB2312、在客戶端通過(guò)escape先編碼再傳,個(gè)心體會(huì)都不是很理想或有些特殊字符不支持。經(jīng)過(guò)比較我決定使用encodeURIComponent在客戶端進(jìn)行編碼,再傳值,除了“/”不支持(但實(shí)際開(kāi)發(fā)中很少需要傳遞該值,如果真有此請(qǐng)況,再加一層判斷即可。
encodeURIComponent的幫助文檔如下:
encodeURIComponent 方法
將文本字符串編碼為一個(gè)統(tǒng)一資源標(biāo)識(shí)符 (URI) 的一個(gè)有效組件。
encodeURIComponent( encodedURIString )
必選的 encodedURIString 參數(shù)代表一個(gè)已編碼的 URI 組件。
說(shuō)明
encodeURIComponent 方法返回一個(gè)已編碼的 URI。如果您將編碼結(jié)果傳遞給 decodeURIComponent ,那么將返回初始的字符串。因?yàn)閑ncodeURIComponent 方法對(duì)所有的字符編碼,請(qǐng)注意,如果該字符串代表一個(gè)路徑,例如 /folder1/folder2/default.html ,其中的斜杠也將被編碼。這樣一來(lái),當(dāng)該編碼結(jié)果被作為請(qǐng)求發(fā)送到 web 服務(wù)器時(shí)將是無(wú)效的。如果字符串中包含不止一個(gè) URI 組件,請(qǐng)使用 encodeURI 方法進(jìn)行編碼。
要求
版本 5.5
請(qǐng)參閱
decodeURI 方法 | decodeURIComponent 方法
應(yīng)用于: Global 對(duì)象
我做了一個(gè)小例子來(lái)展現(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>無(wú)標(biāo)題頁(yè)</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="中華人民共和國(guó)"/>
<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)的漢字簡(jiǎn)繁轉(zhuǎn)換
- javascript 漢字與拼音轉(zhuǎn)換
- javascript漢字轉(zhuǎn)拼音的代碼
- JavaScript實(shí)現(xiàn)ASC轉(zhuǎn)漢字及漢字轉(zhuǎn)ASC的方法
相關(guān)文章
JS獲取本周周一,周末及獲取任意時(shí)間的周一周末功能示例
這篇文章主要介紹了JS獲取本周周一,周末及獲取任意時(shí)間的周一周末功能,結(jié)合實(shí)例形式分析了js通過(guò)擴(kuò)展實(shí)現(xiàn)針對(duì)日期的運(yùn)算相關(guān)技巧,需要的朋友可以參考下2017-02-02JavaScript算法實(shí)例之求二叉樹(shù)從根到葉的所有路徑和
如果你希望求某一特定路徑(例如從根到葉子)上數(shù)字的和,那么問(wèn)題就轉(zhuǎn)變?yōu)榱恕扒蠖鏄?shù)從根到葉的所有路徑和”,所以本文給大家介紹了如何使用JavaScript求二叉樹(shù)從根到葉的所有路徑和,需要的朋友可以參考下2023-10-10ionic實(shí)現(xiàn)滑動(dòng)的三種方式
這篇文章主要為大家詳細(xì)介紹了ionic實(shí)現(xiàn)滑動(dòng)的三種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08JS中的算法與數(shù)據(jù)結(jié)構(gòu)之隊(duì)列(Queue)實(shí)例詳解
這篇文章主要介紹了JS中的算法與數(shù)據(jù)結(jié)構(gòu)之隊(duì)列(Queue),結(jié)合實(shí)例形式詳細(xì)分析了javascript中隊(duì)列的概念、原理、定義及常見(jiàn)操作技巧,需要的朋友可以參考下2019-08-08JavaScript實(shí)現(xiàn)兩個(gè)select下拉框選項(xiàng)左移右移
這篇文章主要介紹了JavaScript實(shí)現(xiàn)兩個(gè)select下拉框選項(xiàng)左移右移功能,js實(shí)現(xiàn)下拉框元素互相移動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03javascript使用substring實(shí)現(xiàn)的展開(kāi)與收縮文字功能示例
這篇文章主要介紹了javascript使用substring實(shí)現(xiàn)的展開(kāi)與收縮文字功能,涉及javascript元素遍歷與屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2019-06-06ES6 Promise對(duì)象的應(yīng)用實(shí)例分析
這篇文章主要介紹了ES6 Promise對(duì)象的應(yīng)用,結(jié)合實(shí)例形式分析了Promise對(duì)象原理與異步處理相關(guān)操作技巧,需要的朋友可以參考下2019-06-06