0

I have created an application for getting the url parameter values using JavaScript, the application is working fine but the problem is that when the param key is having space then i am getting undefined

my code is as given below

script

function getParameter(paramName )
{
 paramName = paramName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
 var regex = new RegExp("[\\?&]" + paramName + "=([^&#]*)", 'i'), results = regex.exec(location.search);
 return results == null ? undefined : decodeURIComponent(results[1].replace(/\+/g, " "));
}

let say the url is like

http://localhost/myapp/index.html?User Id=145&Dept=HR

call

getParameter('Dept') ===> Gives HR
getParameter('User Id') ===> Gives undefined

can anyone please tell me some solution for this

Alex Man
  • 4,746
  • 17
  • 93
  • 178

0 Answers0