• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

getting url variables

Lets say you have the following url

http://www.mydomain.com/info.asp?id=20&name=joe

I am trying to get both values id and name using this function on window.onload or document.ready but its not working.  Here is the function below

$.urlParam = function(name){
	var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
	return results[1] || 0;
}

Open in new window

0
stargateatlantis
Asked:
stargateatlantis
  • 4
  • 4
1 Solution
 
GwynforWebCommented:
location.search returns the search string. This code puts the values in the array results

results=location.search.split('&')
for (i=0;i<results.length;i++)
   results[i] =  results[i].split('=')[1]

Open in new window

0
 
stargateatlantisAuthor Commented:
but the code isn't working
0
 
GwynforWebCommented:
It worked for me. I had an htm page with just this in it:-

<script>

results=location.search.split('&')
for (i=0;i<results.length;i++)
   results[i] =  results[i].split('=')[1]
                                            
alert(results)


</script>

Open in new window

I then opened it with the string ?id=20&name=joe appended and it outputted the required values.

This worked as well

<script>
 function test(){
   results=location.search.split('&')
   for (i=0;i<results.length;i++)
      results[i] =  results[i].split('=')[1]                                         
   alert(results)
 }
</script>

<body onload="test()">
</body>

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
stargateatlantisAuthor Commented:
I know that but i want to extract the URL variables and assign them to other variable names
0
 
stargateatlantisAuthor Commented:
When i execute the function on a on click event it gives me
When using the function

TypeError: results is null
[Break On This Error]       

return results[1] || 0;
0
 
GwynforWebCommented:
I am puzzled why you are doing this client side. To extract then server side you simply do this :-

<%
   idValue = Request.QueryString("id")
   nameValue = Request.QueryString("name")
%>

Open in new window


To assign them to variables available to client side script using server side code you would do this:-

<script>
   idValue =  <%Request.QueryString("id")%>
   nameValue = <%Request.QueryString("name")%>
</script>

Open in new window


I am not entirely sure what you are trying to do, ie is this client or server side. If it is all client side the code I provided before does the job. I have no doubt I can solve this problem for you though I need more details about what you are trying to do and where, ie client side or server side.
0
 
stargateatlantisAuthor Commented:
this is all done using JavaScript.   basically that function is suppose to grab the value of the specified URL variable
0
 
GwynforWebCommented:
Now I fully understand. This does what you want, it is easier and more reliable than using RegExp.

<script>
function urlParam(name){
  results=location.search.replace('?','')
  results=results.split('&')
  for (i=0;i<results.length;i++)
     if (name==results[i].split('=')[0] ) return results[i].split('=')[1]                                         
  return false                                            
 }
</script>

<body onload="alert( urlParam('id')); alert( urlParam('name'))">
</body>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now