setParam.js 607 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Set query string parameter value
  3. */
  4. function setParam(url, paramName, value){
  5. url = url || '';
  6. var re = new RegExp('(\\?|&)'+ paramName +'=[^&]*' );
  7. var param = paramName +'='+ encodeURIComponent( value );
  8. if ( re.test(url) ) {
  9. return url.replace(re, '$1'+ param);
  10. } else {
  11. if (url.indexOf('?') === -1) {
  12. url += '?';
  13. }
  14. if (url.indexOf('=') !== -1) {
  15. url += '&';
  16. }
  17. return url + param;
  18. }
  19. }
  20. module.exports = setParam;