Get URL params with Javascript

/ January 30, 2018/ Javascript

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

Leave a Reply

Be the First to Comment!

Notify of