Posted By

pogosheep on 08/20/10


Tagged


Versions (?)

SplitPageParameters


 / Published in: JavaScript
 

Splits the location.search into an object.

  1. /**
  2.   * Splits the location.search into an object.
  3.   *
  4.   * @param pageParameters The search.
  5.   * @return an Object with the pageparameter.
  6.   *
  7.   * @author Layne Obserdia < [email protected] >
  8.   * @since 2010-09-04
  9.   */
  10. function getPageParameters(pageParameters) {
  11. pageParameters = pageParameters.substr(1).split('&');
  12. var pageParams = {};
  13. var parameters = [];
  14. for (var i = 0, parameter; parameter = pageParameters[i]; i++) {
  15. parameters[i] = parameter.split('=');
  16. }
  17. for (i = 0, parameter; parameter = parameters[i]; i++) {
  18. if (parameter[0].search(/\[\w+\]/) !== -1) {
  19. arrayName = parameter[0].match(/\w+\[/).toString().trim();
  20. var arrayElement = parameter[0].match(/\[\w+\]/).toString().trim();
  21. arrayElement = arrayElement.substr(1, arrayElement.length - 2);
  22. arrayName = arrayName.substr(0, arrayName.length - 1);
  23. if (pageParams[arrayName] === undefined) {
  24. pageParams[arrayName] = {};
  25. }
  26. pageParams[arrayName][arrayElement] = parameter[1];
  27. }
  28. else {
  29. pageParams[parameter[0]] = parameter[1];
  30. }
  31. }
  32. return pageParams;
  33. }

Report this snippet  

You need to login to post a comment.