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.

Ad:


function getUrlParams(key){
    var params={};
    var paramsString = decodeURI(window.location.search).split('?')[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
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments