Advertisement
wertisdk

setGetParameter

Jun 24th, 2014
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. *   Adds or changes a GET parameter
  3. *   See http://stackoverflow.com/a/13064060/703581
  4. *   Adapted to handle '#' in the URL
  5. */
  6. function setGetParameter(paramName, paramValue)
  7. {
  8.     var url = window.location.href;
  9.     var splitAtAnchor = url.split('#');
  10.     url = splitAtAnchor[0];
  11.     var anchor = typeof splitAtAnchor[1] === 'undefined' ? '' : '#' + splitAtAnchor[1];
  12.     if (url.indexOf(paramName + "=") >= 0)
  13.     {
  14.         var prefix = url.substring(0, url.indexOf(paramName));
  15.         var suffix = url.substring(url.indexOf(paramName));
  16.         suffix = suffix.substring(suffix.indexOf("=") + 1);
  17.         suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
  18.         url = prefix + paramName + "=" + paramValue + suffix;
  19.     }
  20.     else
  21.     {
  22.     if (url.indexOf("?") < 0)
  23.         url += "?" + paramName + "=" + paramValue;
  24.     else
  25.         url += "&" + paramName + "=" + paramValue;
  26.     }
  27.     window.location.href = url + anchor;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement