3

Possible Duplicate:
How can I get a specific parameter from location.search?

I have a url like the following.

http://localhost/xxx.php?tPath=&pid=37

I want to get the pid=37 but by its name because on time the url is like above and then when page refreshed then the url becomes like the following.

http://localhost/xxx.php?tPath=&action=xxx&pid=37&value=13&fname=aaaa&fone=4321122

So I want to get the pid=37. It might be a function to which I pass the pid as a parameter and it returns its value.

How will I do this?

Community
  • 1
  • 1
Ahmad
  • 2,099
  • 10
  • 42
  • 79
  • 2
    See this answer: http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript/2880929#2880929 – Linus Thiel Mar 15 '12 at 11:25
  • 1
    The answers of this post can help you. http://stackoverflow.com/questions/979975/how-to-get-the-value-from-url-parameter – matheusvmbruno Mar 15 '12 at 11:27

4 Answers4

13

Take a look at this or follow a solution like this:

function getParam( name )
{
 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
 var regexS = "[\\?&]"+name+"=([^&#]*)";
 var regex = new RegExp( regexS );
 var results = regex.exec( window.location.href );
 if( results == null )
  return "";
else
 return results[1];
}

var frank_param = getParam( 'pid' );
Community
  • 1
  • 1
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
1

Use the following function

function getParameterValue(name)
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ) return "";
else return results[1];
}
Ahmed Hashem
  • 360
  • 1
  • 10
1

Well, there are three answers already, which all use regex. But a regex is really the wrong tool for this job. As someone famously said, some people, when they have a problem, think, "I know, I'll use a regular expression!" Now they have two problems. Here are some of the reasons those answers are wrong:

  • If the "name" you're looking for has regex characters in it, you need to escape them, and all of the regexen fail if the URL uses an ampersand in the path part (not RFC-conformant, but some people might do it anyway).

  • They all fail if the parameter you're looking for has characters that are meaningful in a regex (for example, $).

  • Oh, and they all fail to account for multiple results (e.g., path?foo=bar&foo=baz), which is perfectly valid and relatively common in querystrings.

This really is a job for plain-ol' string functions, as given in this answer. I'd have written the function a little differently stylistically, but the algorithm is sound.

Community
  • 1
  • 1
Josh
  • 992
  • 5
  • 5
0

check this small utility http://jsfiddle.net/gwB2C/2/

usage :

var up = new URLParser();
var urlObj = up.parse('http://localhost/xxx.php?tPath=&action=xxx&pid=37&value=13&fname=aaaa&fone=4321122');
alert(urlObj.params['fname']); //alerts 'aaaa'

value of urlObj :

baseURL: "http://localhost/xxx.php"
params:{
    action: "xxx"
    fname: "aaaa"
    fone: "4321122"
    pid: "37"
    tPath: ""
    value: "13"
}
queryString: "tPath=&action=xxx&pid=37&value=13&fname=aaaa&fone=4321122"
Nemoy
  • 3,347
  • 1
  • 15
  • 14