ParsedQueryString.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*******************************************************************************
  2. *
  3. * ParsedQueryString version 1.0
  4. * Copyright 2007, Jeff Mott <Mott.Jeff@gmail.com>. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms with or without
  7. * modification are permitted provided that the above copyright notice,
  8. * this condition, and the following disclaimer are retained.
  9. *
  10. * THIS SOFTWARE IS PROVIDED AS IS, AND ANY EXPRESS OR IMPLIED WARRANTIES,
  11. * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. * FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. IN NO EVENT SHALL THE
  13. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  14. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING BUT NOT
  15. * LIMITED TO PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  16. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  17. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  19. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. *
  21. ******************************************************************************/
  22. function ParsedQueryString() {
  23. this._init();
  24. }
  25. ParsedQueryString.version = '1.0';
  26. ParsedQueryString.prototype =
  27. {
  28. _init:
  29. function ()
  30. {
  31. this._parameters = {};
  32. if (location.search.length <= 1)
  33. return;
  34. var pairs = location.search.substr(1).split(/[&;]/);
  35. for (var i = 0; i < pairs.length; i++)
  36. {
  37. var pair = pairs[i].split(/=/);
  38. var name = this._decodeURL(pair[0]);
  39. if (Boolean(pair[1]))
  40. {
  41. var value = this._decodeURL(pair[1]);
  42. if (Boolean(this._parameters[name]))
  43. this._parameters[name].push(value);
  44. else
  45. this._parameters[name] = [value];
  46. }
  47. }
  48. },
  49. _decodeURL:
  50. function (url) {
  51. return decodeURIComponent(url.replace(/\+/g, " "));
  52. },
  53. param:
  54. function (name)
  55. {
  56. if (Boolean(this._parameters[name]))
  57. return this._parameters[name][0];
  58. else
  59. return "";
  60. },
  61. params:
  62. function (name)
  63. {
  64. if (Boolean(name))
  65. {
  66. if (Boolean(this._parameters[name]))
  67. {
  68. var values = [];
  69. for (var i = 0; i < this._parameters[name].length; i++)
  70. values.push(this._parameters[name][i]);
  71. return values;
  72. }
  73. else
  74. return [];
  75. }
  76. else
  77. {
  78. var names = [];
  79. for (var name in this._parameters)
  80. names.push(name);
  81. return names;
  82. }
  83. }
  84. };