1. <script language="javascript" type="text/javascript">
  2. /* URL encode / decode */
  3. var Url =
  4. {
  5. // public method for url encoding
  6. encode: function(string) {
  7. return escape(this._utf8_encode(string));
  8. },
  9.  
  10. // public method for url decoding
  11. decode: function(string) {
  12. return this._utf8_decode(unescape(string));
  13. },
  14.  
  15. // private method for UTF-8 encoding
  16. _utf8_encode: function(string) {
  17. string = string.replace(/\r\n/g, "\n");
  18. var utftext = "";
  19.  
  20. for (var n = 0; n < string.length; n++) {
  21.  
  22. var c = string.charCodeAt(n);
  23.  
  24. if (c < 128) {
  25. utftext += String.fromCharCode(c);
  26. }
  27. else if ((c > 127) && (c < 2048)) {
  28. utftext += String.fromCharCode((c >> 6) | 192);
  29. utftext += String.fromCharCode((c & 63) | 128);
  30. }
  31. else {
  32. utftext += String.fromCharCode((c >> 12) | 224);
  33. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  34. utftext += String.fromCharCode((c & 63) | 128);
  35. }
  36.  
  37. }
  38.  
  39. return utftext;
  40. },
  41.  
  42. // private method for UTF-8 decoding
  43. _utf8_decode: function(utftext) {
  44. var string = "";
  45. var i = 0;
  46. var c = c1 = c2 = 0;
  47.  
  48. while (i < utftext.length) {
  49.  
  50. c = utftext.charCodeAt(i);
  51.  
  52. if (c < 128) {
  53. string += String.fromCharCode(c);
  54. i++;
  55. }
  56. else if ((c > 191) && (c < 224)) {
  57. c2 = utftext.charCodeAt(i + 1);
  58. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  59. i += 2;
  60. }
  61. else {
  62. c2 = utftext.charCodeAt(i + 1);
  63. c3 = utftext.charCodeAt(i + 2);
  64. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 ^ 63));
  65. i += 3;
  66. }
  67. }
  68. return string;
  69. }
  70. }
  71.  
  72. </script>