javascript URL編碼和解碼使用說明

來源:互聯網
上載者:User

在有些傳遞頁面使用GB2312,而在接收頁面使用 UTF8,這樣接收到的參數就可能會與原來發生不一致。使用伺服器端的urlEncode函數編碼的URL,與使用用戶端javascript的 encodeURI函數編碼的URL,結果就不一樣。
javaScript中的編碼方法:
escape() 方法:
採用ISO Latin字元集對指定的字串進行編碼。所有的空格符、標點符號、特殊字元以及其他非ASCII字元都將被轉化成%xx格式的字元編碼 (xx等於該字元在字元集表裡面的編碼的16進位數字)。比如,空格符對應的編碼是%20。unescape方法與此相反。不會被此方法編碼的字 符: @ * / +

英文解 釋:MSDN JScript Reference: The escape method returns a string value (in Unicode format) that contains the contents of [the argument].
All spaces, punctuation, accented characters, and any other non- ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character.
For example, a space is returned as "%20."
Edge Core Javascript Guide: The escape and unescape functions let you encode and decode strings.
The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set.
The unescape function returns the ASCII string for the specified hexadecimal encoding value.

encodeURI() 方法:把URI字串採用UTF-8編碼格式轉化成escape格式的字串。不會被此方法編碼的字元:! @ # $& * ( ) = : / ; ? + '

英文解 釋:MSDN JScript Reference: The encodeURI method returns an encoded URI.
If you pass the result to decodeURI, the original string is returned.
The encodeURI method does not encode the following characters: ":", "/", ";", and "?".
Use encodeURIComponent to encode these characters. Edge Core Javascript Guide: Encodes a Uniform Resource
Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF- 8 encoding of the character

encodeURIComponent() 方法:把URI字串採用UTF-8編碼格式轉化成escape格式的字串。與encodeURI()相 比,這個方法將對更多的字元進行編碼,比如 / 等字元。所以如果字串裡麵包含了URI的幾個部分的話,不能用這個方法來進行編碼,否則 / 字元被編 碼之後URL將顯示錯誤。不會被此方法編碼的字元:! * ( )

英文解 釋:MSDN JScript Reference: The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned.
Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1 /folder2 /default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a
single URI component. Mozilla Developer Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one,
two, or three escape sequences representing the UTF- 8 encoding of the character.

因此,對於中文字串來說,如果不希望把字串編碼格式轉化成UTF-8格式的(比如原頁面和目標頁面 的charset是一致的時候),只需要使用escape。如果你的頁面是GB2312或者其他的編碼,而接受參數的頁面是UTF-8編碼的,就要採用 encodeURI或者encodeURIComponent。
另外,encodeURI/encodeURIComponent是在javascript1.5之後引進的,escape則在javascript1.0版本就有。

英文注 釋:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields.
Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().
Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring,
which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un- encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in most
cases when encoding
a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included.
Note that this method
does not encode the ' character, as it is a valid character within URIs.

1.編碼處理函數
1) encodeURI 返回一個對URI字串編碼後的結果。URL是最常見的一種URI;
2) decodeURI 將一個已編碼的URI字串解碼成最原始的字串返回;
3) 舉例: < Script language = " javascript " > 輸出結果如下: encodeStr: http://www.amigoxie.com/index.jsp?name=%E9%98%BF%E8%9C%9C%E6%9E%9C decodeStr: http://www.amigoxie.com/index.jsp?name=xind
2. 數值處理函數
1) parseInt 將一個字串指定的進位轉換為一個整數,文法格式為: parseInt(numString, [radix]) 第一個參數是要進行轉換的字串,是介於2到36之間的數值,用於指定進行字串轉換時所用的進位。 舉例如下: 輸出結果如下: 預設情況下的結果:32:32;032:26;0x32:50 轉為2進位的結果:32:NaN;032:0;0x32:0 轉為8進位的結果:32:26;032:26;0x32:0 轉為16進位的結果:32:50;032:50;0x32:50 11001010轉換後的結果: 2進位:202;16進位:285216784 8進位:2359816;10進位:11001010 43abc轉換後:43;abc43轉換後:NaN;abc轉換後:NaN
2) parseFloat方法 該方法將一個字串轉換成對應的小數。 eg. 輸出結果如下: 4.11 5.1 3) isNaN方法 該方法用於檢測前兩個方法傳回值是否為非數值型,如果是,返回true,否則,反回false

相關文章

聯繫我們

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