js中encode、decode的應用說明_javascript技巧

來源:互聯網
上載者:User
encodeURIComponent

將文本字串編碼為一個統一資源識別項 (URI) 的一個有效組件。

encodeURIComponent(encodedURIString)

必選的encodedURIString參數代表一個已編碼的 URI 組件。

說明
encodeURIComponent方法返回一個已編碼的 URI。如果您將編碼結果傳遞給decodeURIComponent,那麼將返回初始的字串。因為encodeURIComponent方法對所有的字元編碼,請注意,如果該字串代表一個路徑,例如/folder1/folder2/default.html,其中的斜杠也將被編碼。這樣一來,當該編碼結果被作為請求發送到 網頁伺服器時將是無效的。如果字串中包含不止一個 URI 組件,請使用encodeURI方法進行編碼。

escape 方法
返回一個可在所有電腦上讀取的編碼 String 對象。

function escape(charString : String) : String
參數
charString
必選。要編碼的任何 String 對象或文本。
備忘
escape 方法返回一個包含 charstring 內容的字串值(Unicode 格式)。所有空格、標點、重音符號以及任何其他非 ASCII 字元都用 %xx 編碼替換,其中 xx 等於表示該字元的十六進位數。例如,空格返回為“ ”。

字元值大於 255 的字元以 %uxxxx 格式儲存。

注意 escape 方法不能用來對“統一資源識別項”(URI) 進行編碼。對其編碼應使用 encodeURI 和 encodeURIComponent 方法。
要求
版本 1

請參見
encodeURI 方法 | encodeURIComponent 方法 | String 對象 | 方法

適用於:Global 對象

encodeURI 方法
返回編碼為有效統一資源識別項 (URI) 的字串。

function encodeURI(URIString : String) : String
參數
URIString
必選。表示編碼 URI 的字串。
備忘
encodeURI 方法返回一個已編碼的 URI。如果將編碼結果傳遞給 decodeURI,則將返回初始的字串。encodeURI 不對下列字元進行編碼:“:”、“/”、“;”和“?”。請使用 encodeURIComponent 對這些字元進行編碼。

要求
版本 5.5

請參見
decodeURI 方法 | decodeURIComponent 方法

適用於:Global 對象

encodeURIComponent 方法
返回編碼為統一資源識別項 (URI) 的有效組件的字串。

function encodeURIComponent(encodedURIString : String) : String
參數
encodedURIString
必選。表示編碼 URI 組件的字串。
備忘
encodeURIComponent 方法返回一個已編碼的 URI。如果將編碼結果傳遞給 decodeURIComponent,則將返回初始的字串。因為 encodeURIComponent 方法將對所有字元編碼,請注意,如果該字串代表一個路徑,例如 /folder1/folder2/default.html,則其中的斜杠也將被編碼,這樣,當該字串作為請求發送到 Web 服務器時它將是無效的。如果字串中包含多個 URI 組件,請使用 encodeURI 方法進行編碼。

要求
版本 5.5

請參見
decodeURI 方法 | decodeURIComponent 方法

適用於:Global 對象

方法
從用 escape 方法編碼的 String 對象中返回已解碼的字串。

function (charString : String) : String
參數
charString
必選。要解碼的 String 對象或文本。
備忘
方法返回一個包含 charstring 內容的字串值。所有以 %xx 十六進位形式編碼的字元都用 ASCII 字元集當中等效的字元代替。

以 %uxxxx 格式(Unicode 字元)編碼的字元用十六進位編碼 xxxx 的 Unicode 字元代替。

注意 方法不應用於解碼“統一資源識別項”(URI)。請改用 decodeURI 和 decodeURIComponent 方法。
要求
版本 1

請參見
decodeURI 方法 | decodeURIComponent 方法 | escape 方法 | String 對象

適用於:Global 對象

decodeURI 方法
返回一個已編碼的統一資源識別項 (URI) 的非編碼形式。

function decodeURI(URIstring : String) : String
參數
URIstring
必選。表示編碼 URI 的字串。
備忘
使用 decodeURI 方法代替已經過時的 方法。

decodeURI 方法返回一個字串值。

如果 URIString 無效,將發生 URIError。

要求
版本 5.5

請參見
decodeURIComponent 方法 | encodeURI 方法

適用於:Global 對象

decodeURIComponent 方法
返回統一資源識別項 (URI) 的一個已編碼組件的非編碼形式。

function decodeURIComponent(encodedURIString : String) : String
必選的 encodedURIString 參數是一個表示已編碼的 URI 組件的值。

備忘
URIComponent 是一個完整的 URI 的一部分。

如果 encodedURIString 無效,則將產生 URIError。

要求
版本 5.5

請參見
decodeURI 方法 | encodeURI 方法

[html] <!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> <title>Js encode,decode</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </head> <body> <script> a=62; function encode() { var code = document.getElementById(''code'').value; code = code.replace(/[\r\n]+/g, ''''); code = code.replace(/''/g, "\\''"); var tmp = code.match(/\b(\w+)\b/g); tmp.sort(); var dict = []; var i, t = ''''; for(var i=0; i<tmp.length; i++) { if(tmp[i] != t) dict.push(t = tmp[i]); } var len = dict.length; var ch; for(i=0; i<len; i++) { ch = num(i); code = code.replace(new RegExp(''\\b''+dict[i]+''\\b'',''g''), ch); if(ch == dict[i]) dict[i] = ''''; } document.getElementById(''code'').value = "eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'''':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''''.replace(/^/,String)){while(c--)d[e(c)]=k[c]||e(c);k=[function(e){return d[e]}];e=function(){return''\\\\w+''};c=1};while(c--)if(k[c])p=p.replace(new RegExp(''\\\\b''+e(c)+''\\\\b'',''g''),k[c]);return p}(" + "''"+code+"'',"+a+","+len+",''"+ dict.join(''|'')+"''.split(''|''),0,{}))"; } function num(c) { return(c<a?'''':num(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36)); } function run() { eval(document.getElementById(''code'').value); } function decode() { var code = document.getElementById(''code'').value; code = code.replace(/^eval/, ''''); document.getElementById(''code'').value = eval(code); } </script> <textarea id=code cols=80 rows=20> </textarea><br> <input type=button onclick=encode() value=編碼> <input type=button onclick=run() value=執行> <input type=button onclick=decode() value=解碼> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

[/html]

更多對照可以參考下面的這篇文章 JavaScript 全域對象參考手冊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.