Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Do you observe that you url localhost/myapp/index.html?User Id=145&Dept=HR actually is encoded into "localhost/myapp/index.html?User%20Id=145&Dept=HR"; ?? –  TechMa9iac Aug 23 '14 at 11:56
    
so i need to call like getParameter('User%20Id') –  Alex Man Aug 23 '14 at 11:57
    
in IE its not working –  Alex Man Aug 23 '14 at 12:00
    
This is a FAQ and duplicate stackoverflow.com/questions/979975/… –  mplungjan Aug 23 '14 at 12:03
    
Also spaces are not normal in a parameter name –  mplungjan Aug 23 '14 at 12:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.