Monday
Dec202004
Get URL Parameter in Javascript
Monday, December 20, 2004 at 12:16 Just a useful little javascript function which will get a URL parameter and return it to you. For example if the current URL is "...?opendocument&id=testid" then calling getURLParam("id") will return "testid".
function getURLParam(strParamName){
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
}
return unescape(strReturn);
}
Update (31st December 2006)
This posting has been around for a long time now and gets a lot of traffic. Although it will still work fine as long as you know its limitations, several people have pointed to this article as being a better solution. I totally agree, it's well worth a look.
Update (27th February 2007)
A couple of bug fixes raised via email. Thanks to Oliver Bryant for those.
Comments Off | |
Permalink
Permalink


Reader Comments (52)
if ( strHref.indexOf("&") > -1 ) SHOULD BE
if ( strHref.indexOf("?") > -1 )
strHref.substr(strHref.indexOf("&")) OF THE FOLLOWING LINE SHOULD BE
strHref.substr(strHref.indexOf("?"))
SO the param string will start searching for params and valus with the first parameter always following a ? character and split subsequent pairs with the & character.
Thank you for this code, it is the best javascript solution I have found yet.
You are correct of course. This was written for a Domino application where the first parameter after the ? can always be ignored. Thanks for pointing it out though.
Matt
Hi
http://www.11tmr.com/11tmr.nsf/emoticons/DLYH-5N3GK5/$FILE/huh.gif">
Nice work and i have saved the same thing as seperate java script file and i am calling that script through right click IE menu , It's not working there why? ie. It's not returning the URL ?
Not sure what the problem is. If you create a shortcut with this as a value:
javascript:alert(document.location.href)
It seems to work fine (i.e. you get a prompt with the current URL in it) so it should just be a matter of changing to code to do what you want from there.
Hey Tom,
I was getting syntax errors when I called the function from a form:
onLoad="document.myForm.myfield.value = getURLParam('id')"
The breakthrough was that I used single quotes instead of double quotes in the parameter I was passing, because of the external double quotes.
OK,
nice code by the way I'm working with something very similar, I'm not a javascript kind of person so go easy :)
I'm trying to do the same thing but passing an array in the params. i.e.
blah.cgi?foo=bar&foo=bah&foo=baz&feh=bam
So I end up with foo=(bar,bah,baz)&feh=(bam)
can anyone point me in the right direction?
It should be no problem psyber. Just don't use the brackets. Use my code as is and then once you have the value of the parameter called "foo" (which will be a string with the value "bar,bah,baz"), you then just need to "split" the sting into an array:
var strValue = getURLParam("foo");
var myArray = strValue.split(",");
Hope this helps
Matt
There is a bug on the script or ruther the way your are defining a URL parameter is not actually the formal definition of URL parameter. A URL parameter is anything after the ? in the URL
The bug is that it wont find id in the the following URL
".htm?id=testid¶m2=foo"
small modification to your code Matt:
just changed the first two occurences of & to ?
---------------------------------------------------------------------------
function getURLParam(strParamName){
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
}
return strReturn;
}
Giorgos,
As mentioned in the first two comments this function was written for a Lotus Domino application where the first parameter after the ? can always be ignored (it is an internal server parameter). However, it seems that most people coming here these days are not Domino heads so I have updated the function to reflect a more generic requirement.
Matt
Fantastic and simple to implement! I've been searching for this solution for months.
I'm Perl efficient and Javascript challenged. Unfortunate for me, the site owner requested a complex javascript menu. In order to store the javascript menu source outside the html file, I needed to pull a value from the URL params before loading the javascript menu arrays.
The address I entered above is the active site for Quantity Postcards. The URL where the new site is being tested is http://www.qpfans.com/catalog/psdbi/intro.html?TEMP_PARAM1=retail" rel="nofollow">http://www.qpfans.com/catalog/psdbi/intro.html?TEMP_PARAM1=retail
Thank you, Matt, for using the Internet to share your knowledge. I will include your name and a site link.
Ashford
NOTICE:
The variable name should be lower cases. I had a variable link.asp?SomethiNG=1234 and with
getURLParam('SomethiNG'); // DOES NOT WORK
getURLParam('something'); // WORKS
Hope it helps
<script language="JavaScript">alert();</script>
Very fine .
Hi
I am new to javascript.
Where is the code placed on the html page?
ta
James
Hi James,
You can place Javascript anywhere in the HTML as long as it is surrounded with:
<script language="javascript">
//Your script goes here
</script>
Thant being said normally JS will be placed in the HTML header or in a seperate file which would be referenced using:
<script language="javascript" src="myscript.js"></script>
Hope this helps.
Matt
Thank you very much! I'm lookine for url parsrer for few days already! I've got completely mad!
Thanks, your function helped me a lot, you´re welcome to come to México anytime.
Wow. Thanks! I've looked everywhere for a good javascript version of the php $_GET. This works wonderfully! Well done!!
http://www.11tmr.com/11tmr.nsf/emoticons/DLYH-5N3GJN/$FILE/cool.gif">
Hello, just a little improvment ...
You should also use lowercase for the parameter of your function ...
line -> if (aQueryString[iParam].indexOf(strParamName.toLowerCase()+ "=") > -1 ) .. or a call to getUrlParam('Foo') won't work in a url ...?Foo=bar....
I am having troubles getting it across frames.
[frame 1]
[frame 2]
how can i read the url from frame 1 in frame 2?
This is a little improvement when using frames
function GetURLParam(strParamName,FrameNaam)
{ var strReturn = "";
var strHref = parent.frames[FrameNaam].location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if ( aQueryString[iParam].indexOf(strParamName + "=") > -1 )
{ var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
}
return strReturn;
}
Hi guys, I've got a little javascript question here, i have used your script to get the url parameter i want. This is the url:
blababla/en/index.php?frames=&views=statistics(1460)&functions=&elementId=712&action=browse
so i get the views in return as a value: statistics(1460)
but then, how can i get only the number out of it? so as a result tot get 1460 instead of statistics(1460) ??
Thanks for your time :)
statistics = "statistics(1460)";
stat2 = statistics.split("statistics(");
statistics = stat2.join("");
stat2 = statistics.split(")");
statistics = stat2.join("");
alert(statistics);
I know there's probably a much better and simpler was to do this (which i don't know yet), but this should work...
And here is the simple way... (sorry for double post)
views = getURLParam('views');
viewsnumber = views.substring(views.indexOf("(")+1,views.indexOf(")"));
Excelente codigo solo le cambie algunas cosas para usarlo con paginas ASP
<script language="JavaScript" type="text/javascript">
function getURLParam(strParamName){
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if (aQueryString[iParam].indexOf("=") > -1 )
{
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
}
return strReturn;
}
</script>
Gracias MATT