Posted By

fugue on 12/29/06


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


Parse form date


 / Published in: JavaScript
 

  1. <script language="JavaScript">
  2. <!--
  3.  
  4. /*
  5. Webmonkey GET Parsing Module
  6. Language: JavaScript 1.0
  7.  
  8. The parsing of GET queries is fundamental
  9. to the basic functionality of HTTP/1.0.
  10. This module parses GET with JavaScript 1.0.
  11.  
  12. Source: Webmonkey Code Library
  13. (http://www.hotwired.com/webmonkey/javascript/code_library/)
  14.  
  15. Author: Patrick Corcoran
  16. Author Email: [email protected]
  17. */
  18.  
  19. function createRequestObject() {
  20.  
  21. FORM_DATA = new Object();
  22. // The Object ("Array") where our data will be stored.
  23.  
  24. separator = ',';
  25. // The token used to separate data from multi-select inputs
  26.  
  27. query = '' + this.location;
  28. // Get the current URL so we can parse out the data.
  29. // Adding a null-string '' forces an implicit type cast
  30. // from property to string, for NS2 compatibility.
  31.  
  32. query = query.substring((query.indexOf('?')) + 1);
  33. // Keep everything after the question mark '?'.
  34.  
  35. if (query.length < 1) { return false; } // Perhaps we got some bad data?
  36.  
  37. keypairs = new Object();
  38. numKP = 1;
  39. // Local vars used to store and keep track of name/value pairs
  40. // as we parse them back into a usable form.
  41.  
  42. while (query.indexOf('&') > -1) {
  43. keypairs[numKP] = query.substring(0,query.indexOf('&'));
  44. query = query.substring((query.indexOf('&')) + 1);
  45. numKP++;
  46. // Split the query string at each '&', storing the left-hand side
  47. // of the split in a new keypairs[] holder, and chopping the query
  48. // so that it gets the value of the right-hand string.
  49. }
  50.  
  51. keypairs[numKP] = query;
  52. // Store what's left in the query string as the final keypairs[] data.
  53.  
  54. for (i in keypairs) {
  55. keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
  56. // Left of '=' is name.
  57. keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
  58. // Right of '=' is value.
  59. while (keyValue.indexOf('+') > -1) {
  60. keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
  61. // Replace each '+' in data string with a space.
  62. }
  63.  
  64. keyValue = unescape(keyValue);
  65. // Unescape non-alphanumerics
  66.  
  67. if (FORM_DATA[keyName]) {
  68. FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
  69. // Object already exists, it is probably a multi-select input,
  70. // and we need to generate a separator-delimited string
  71. // by appending to what we already have stored.
  72. } else {
  73. FORM_DATA[keyName] = keyValue;
  74. // Normal case: name gets value.
  75. }
  76. }
  77.  
  78. return FORM_DATA;
  79. }
  80.  
  81. FORM_DATA = createRequestObject();
  82. // This is the array/object containing the GET data.
  83. // Retrieve information with 'FORM_DATA [ key ] = value'.
  84.  
  85.  
  86. // -->
  87.  
  88. </script>

Report this snippet  

You need to login to post a comment.