Get URL params with Javascript

/ January 30, 2018/ Javascript

(Last Updated On: February 12, 2018) This function will return an Object with the URL Params (also know as Search Params) and their values.

It also will recover just the value of one specific param or undefined if the param does not exist or does not have a value.


function getUrlParams(key){
    var params={};
    var paramsString = decodeURI('?')[1].split('&');
    for ( var i = 0 ; i < paramsString.length ; i++) {
        var keyVal = paramsString[i].split('=');
        params[keyVal[0]] = keyVal[1]
    return key? params[key] : params;
  • Get Object with all params: getUrlParams();
  • Get one param (here id) : getUrlParams('id')
Spread the love
Notify of
Inline Feedbacks
View all comments