Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Changing URL parameters using Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 351
    Comment on it

    More often than not we need to change URL parameters sometimes for Hash tags and sometimes for creating url parameters for creating new clickable URLs..

    function updateQueryStringParameter(uri, key, value) {
      var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
      var separator = uri.indexOf('?') !== -1 ? "&" : "?";
      if (uri.match(re)) {
        return uri.replace(re, '$1' + key + "=" + value + '$2');
      }
      else {
        return uri + separator + key + "=" + value;
      }
    }
    

    The above function simply uses regular expressions, to look for & and ? and query sting concatenated with key we are trying to replace, if the match is found it replaces it else simply concatenates the new parameter.. The above function is taken from the link...

    http://stackoverflow.com/questions/5999118/add-or-update-query-string-parameter

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: