Advertisement
manman89

JAVASCRIPT - URL ENCODE BASED ON RFC 3986

Feb 9th, 2013
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. http://blogger.hide.vn
  2.  
  3. ================
  4.  
  5. Several variations of the URL encoding are used today:
  6.  
  7. RFC 3986 URI Encoding is defined in RFC 3986 - "Uniform Resource Identifier (URI): Generic Syntax", http://tools.ietf.org/html/rfc3986. See the previous section for details.
  8.  
  9. Application/x-www-form-urlencoded Encoding is the same as RFC 3986 URI Encoding except that:
  10.     Space characters ( ) are replaced by plus sign characters (+).
  11.    
  12. RFC 1738 URL Encoding is defined in RFC 1738 - "Uniform Resource Locators (URL)", http://tools.ietf.org/html/rfc1738.
  13. RFC 1738 URL Encoding is the same as RFC 3986 URI Encoding except that:
  14.     Characters outside the URL character set are not required to be converted using UTF-8 schema. Any other schemas can be used to convert characters to byte sequences. Then each byte of the sequence is encoded as %xx, where xx is a pair of hexadecimal digits representing that byte.
  15.  
  16. ================
  17.  
  18. function URL_ENCODE_RFC_3986(DATA,METHOD){
  19.   var RESULT = "";
  20.   var SUPPORT_METHOD = ["GET","POST"];
  21.  
  22.   if(typeof(DATA)!="string"){return "REQUIRE DATA IS STRING";}
  23.   if(typeof(METHOD)!="string"){return "REQUIRE METHOD IS STRING";}  
  24.  
  25.   DATA = Trim(DATA.toString());
  26.   METHOD = Trim(METHOD.toString());
  27.  
  28.   if(DATA=="" || METHOD==""){return "DATA OR METHOD IS MISSING";}
  29.  
  30.   METHOD = METHOD.toUpperCase();
  31.   if(SUPPORT_METHOD.indexOf(METHOD)==-1){return "METHOD NOT SUPPORT";}
  32.  
  33.   var vmkreplace = {
  34.     "!": "%21",
  35.     "*": "%2A",
  36.     "'": "%27",
  37.     "(": "%28",
  38.     ")": "%29"
  39.   };
  40.   RESULT = encodeURIComponent(DATA);
  41.   for(var val in vmkreplace){RESULT = RESULT.split(val).join(vmkreplace[val]);}
  42.   if(METHOD=="POST"){RESULT = RESULT.replace(/%20/g,'+');}
  43.  
  44.   return RESULT;
  45. }
  46. function Trim(string){
  47.   if(typeof(string) === "undefined"){return ""}
  48.   return string.toString().replace(/^\s\s*/,'').replace(/\s\s*$/,'');
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement