一輩子的程序員?

          愛你一生不變-芳芳!
          posts - 27, comments - 15, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          escape() & encodeURI() & encodeURIComponent()

          Posted on 2006-07-19 10:00 boddi 閱讀(544) 評論(0)  編輯  收藏

          escape() & encodeURI() & encodeURIComponent()

          escape() & encodeURI() & encodeURIComponent()這三個函數都可以用來對URI進行encode或過濾特殊字符(#/$&+=?/等)。我的經驗是最好用encodeURIComponent()(需要IE 5.5以上,FireFox當然沒問題),因為對UTF-8支持比較好,不會遇到中文亂碼問題,否則還需要進行編碼轉換,很麻煩的。Terac Rome就是用的encodeURIComponent(),del.icio.us和365key用的是escape()。當然,有人這三個函數都不用,自己寫過濾函數,我覺得那純粹是浪費精力。

          下面是MSDN上對這三個函數的解釋:

          escape(charString)

          The escape method returns a string value (in Unicode format) that contains the contents of charstring. 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."

          Characters with a value greater than 255 are stored using the %uxxxx format.

          Note?? The escape method should not be used to encode Uniform Resource Identifiers (URI). Use encodeURI and encodeURIComponent methods instead.

          http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthescape.asp

          ?encodeURI(URIString)

          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.

          http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthfencodeuri.asp

          encodeURIComponent(encodedURIString)

          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.

          http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthencodeuricomponent.asp
          encodeURIComponent 方法返回一個已編碼的 URI。如果將編碼結果傳遞給 decodeURIComponent,則將返回初始的字符串。因為 encodeURIComponent 方法將對所有字符編碼,請注意,如果該字符串代表一個路徑,例如 /kyle/blogpost.asp,則其中的斜杠也將被編碼,這樣,當該字符串作為請求發送到 Web 服務器時它將是無效的。如果字符串中包含多個 URI 組件,請使用 encodeURI 方法進行編碼。
          【例子】encodeURIComponent('/?&神泥') 返回 %2F%3F%26%E7%A5%9E%E6%B3%A5

          encodeURI 方法返回一個已編碼的 URI。如果將編碼結果傳遞給 decodeURI,則將返回初始的字符串。encodeURI 不對下列字符進行編碼:“:”、“/”、“;”和“?”。請使用 encodeURIComponent 對這些字符進行編碼。
          【例子】encodeURI('/?&神泥') 返回 /?&%E7%A5%9E%E6%B3%A5


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 贵阳市| 浮梁县| 青龙| 铜鼓县| 岢岚县| 金塔县| 衡水市| 丹东市| 南阳市| 渑池县| 资源县| 江川县| 江口县| 崇阳县| 余江县| 新昌县| 运城市| 裕民县| 三亚市| 云和县| 高雄县| 湘乡市| 定州市| 连州市| 鄂伦春自治旗| 大同县| 紫阳县| 开化县| 筠连县| 河北省| 莱西市| 神木县| 辽阳市| 茌平县| 广南县| 宁强县| 陇南市| 襄汾县| 溧阳市| 朝阳市| 舒兰市|