javascript URL編碼和解碼使用說明
在有些傳遞頁(yè)面使用GB2312,而在接收頁(yè)面使用 UTF8,這樣接收到的參數(shù)就可能會(huì)與原來發(fā)生不一致。使用服務(wù)器端的urlEncode函數(shù)編碼的URL,與使用客戶端javascript的 encodeURI函數(shù)編碼的URL,結(jié)果就不一樣。
javaScript中的編碼方法:
escape() 方法:
采用ISO Latin字符集對(duì)指定的字符串進(jìn)行編碼。所有的空格符、標(biāo)點(diǎn)符號(hào)、特殊字符以及其他非ASCII字符都將被轉(zhuǎn)化成%xx格式的字符編碼 (xx等于該字符在字符集表里面的編碼的16進(jìn)制數(shù)字)。比如,空格符對(duì)應(yīng)的編碼是%20。unescape方法與此相反。不會(huì)被此方法編碼的字 符: @ * / +
英文解 釋: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編碼格式轉(zhuǎn)化成escape格式的字符串。不會(huì)被此方法編碼的字符:! @ # $& * ( ) = : / ; ? + '
英文解 釋: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編碼格式轉(zhuǎn)化成escape格式的字符串。與encodeURI()相 比,這個(gè)方法將對(duì)更多的字符進(jìn)行編碼,比如 / 等字符。所以如果字符串里面包含了URI的幾個(gè)部分的話,不能用這個(gè)方法來進(jìn)行編碼,否則 / 字符被編 碼之后URL將顯示錯(cuò)誤。不會(huì)被此方法編碼的字符:! * ( )
英文解 釋: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.
因此,對(duì)于中文字符串來說,如果不希望把字符串編碼格式轉(zhuǎn)化成UTF-8格式的(比如原頁(yè)面和目標(biāo)頁(yè)面 的charset是一致的時(shí)候),只需要使用escape。如果你的頁(yè)面是GB2312或者其他的編碼,而接受參數(shù)的頁(yè)面是UTF-8編碼的,就要采用 encodeURI或者encodeURIComponent。
另外,encodeURI/encodeURIComponent是在javascript1.5之后引進(jìn)的,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.編碼處理函數(shù)
1) encodeURI 返回一個(gè)對(duì)URI字符串編碼后的結(jié)果。URL是最常見的一種URI;
2) decodeURI 將一個(gè)已編碼的URI字符串解碼成最原始的字符串返回;
3) 舉例: < Script language = " javascript " > 輸出結(jié)果如下: 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. 數(shù)值處理函數(shù)
1) parseInt 將一個(gè)字符串指定的進(jìn)制轉(zhuǎn)換為一個(gè)整數(shù),語法格式為: parseInt(numString, [radix]) 第一個(gè)參數(shù)是要進(jìn)行轉(zhuǎn)換的字符串,是介于2到36之間的數(shù)值,用于指定進(jìn)行字符串轉(zhuǎn)換時(shí)所用的進(jìn)制。 舉例如下: 輸出結(jié)果如下: 默認(rèn)情況下的結(jié)果:32:32;032:26;0x32:50 轉(zhuǎn)為2進(jìn)制的結(jié)果:32:NaN;032:0;0x32:0 轉(zhuǎn)為8進(jìn)制的結(jié)果:32:26;032:26;0x32:0 轉(zhuǎn)為16進(jìn)制的結(jié)果:32:50;032:50;0x32:50 11001010轉(zhuǎn)換后的結(jié)果: 2進(jìn)制:202;16進(jìn)制:285216784 8進(jìn)制:2359816;10進(jìn)制:11001010 43abc轉(zhuǎn)換后:43;abc43轉(zhuǎn)換后:NaN;abc轉(zhuǎn)換后:NaN
2) parseFloat方法 該方法將一個(gè)字符串轉(zhuǎn)換成對(duì)應(yīng)的小數(shù)。 eg. 輸出結(jié)果如下: 4.11 5.1 3) isNaN方法 該方法用于檢測(cè)前兩個(gè)方法返回值是否為非數(shù)值型,如果是,返回true,否則,反回false
相關(guān)文章
javascript 變速加數(shù)功能實(shí)現(xiàn)代碼
試想一下你要在你的網(wǎng)站提供如下這樣的功能:提供一個(gè)文本框用于收集用戶數(shù)據(jù),這個(gè)文本框只能接受整型的數(shù)值,不提供給用戶手工輸入,只提供兩個(gè)按鈕。2009-10-10js實(shí)現(xiàn)select組件的選擇輸入過濾代碼
如何實(shí)現(xiàn)select組件的選擇輸入過濾作用,下面有一段js代碼,很實(shí)用,需要的朋友可以看看2014-10-10基于layui數(shù)據(jù)表格以及傳數(shù)據(jù)的方式
今天小編就為大家分享一篇基于layui數(shù)據(jù)表格以及傳數(shù)據(jù)的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08- 向?qū)Э梢宰屇愕木W(wǎng)站用戶快速上手使用你的web應(yīng)用,提高網(wǎng)站的吸引力。向?qū)б话惴譃楹脦讉€(gè)步驟,每個(gè)步驟收集一些數(shù)據(jù),并且支持退回功能,所有步驟完成后可以得到每一步的收集結(jié)果。這里給大家展示一種比較通用,靈活且簡(jiǎn)單的向?qū)Э蚣堋?/div> 2011-08-08
用javascript將數(shù)據(jù)導(dǎo)入Excel示例代碼
將數(shù)據(jù)導(dǎo)入Excel的方法有很多,本例介紹的這個(gè)是使用js來實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入,感興趣的朋友可以了解下2014-09-09javascript實(shí)現(xiàn)根據(jù)iphone屏幕方向調(diào)用不同樣式表的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)根據(jù)iphone屏幕方向調(diào)用不同樣式表的方法,涉及javascript針對(duì)樣式表動(dòng)態(tài)操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-07-07Selenium執(zhí)行JavaScript腳本的方法示例
這篇文章主要介紹了Selenium執(zhí)行JavaScript腳本的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12微信小程序動(dòng)畫(Animation)的實(shí)現(xiàn)及執(zhí)行步驟
這篇文章主要介紹了微信小程序動(dòng)畫(Animation) 的實(shí)現(xiàn)及執(zhí)行步驟,需要的朋友可以參考下2018-10-10最新評(píng)論