The Netlobo logo - a Nevada desert landscape

Comments for Get URL Parameters Using Javascript

< Back to the article

48 comments for this article.

RSS Feed Icon Subscribe to the comments for this article

Posted: 2007-07-10 15:05:04 by Andy
What about using location.search ?
See this article for an example...

adamv.com/dev/javascript/querystring
Posted: 2007-07-10 15:15:44 by lobo235 - Netlobo Staff Member
This looks like a good way to do it too but I can see a couple disadvantages to doing it this way.

1. That code does not properly handle URLs with an anchor in them like:
http://www.test.com/i.html?b=1&c=2#34
What happens when you try to get the 'c' parameter is that it gives you 2#34 instead of just the number 2. The function I wrote handles this case properly.

2. The code you reference is also more lines of code which causes it to load slower and use more bandwidth, etc. I like to keep things short and simple.
Posted: 2007-07-12 19:05:40 by Milton
I like your code... it's useful and well thought... cheers and keep doing the good work!
Posted: 2007-09-12 16:42:15 by Serge
I was searching how to do this properly width RegExp and the pattern was not so easy for me.
Thank you very much for your code ! Very clean and clear.
Posted: 2007-09-13 13:51:42 by joe
That's exactly what I was looking for. Well Done!!
Posted: 2007-09-19 14:24:39 by Urban
The perfect solution! Witout any must-read-this-long-article-first. Beutiful! Thx.
Posted: 2007-09-27 14:25:18 by lode
Great piece of coding.

But,... i have one question.
What if the number of parameters (and their names) is unknown?

How would i solve that problem, using your code of course.

Greetz, Lode
Posted: 2007-09-27 14:53:20 by lobo235 - Netlobo Staff Member
Well, that particular problem is out of the scope of this article but you should be able to get all the parameter names using the following function which will return an Array of all parameter names. You can then lookup each parameter once you know the names.

function gpn( ) // gpn stands for 'get parameter names'
{
	var params = new Array( );
	var regex = /[\?&]([^=]+)=/g;
	while( ( results = regex.exec( window.location.href ) ) != null )
		params.push( results[1] );
	return params;
}
Posted: 2007-09-27 14:59:59 by Relic
Okay, so I was trying to use it and it's trhowing an error around the "name.replace()" it's saying its not a function. what's wrong here?
Posted: 2007-09-27 15:01:38 by lobo235 - Netlobo Staff Member
Which browser did you encounter this problem with? I have tested it with recent versions of Firefox, Opera, and IE and they all seem to work.
Posted: 2007-10-15 09:01:51 by Shubhangi
Hi,
Thanks, for your solution.
The perfect solution!!!!
This solved my most criticle problem. Only I had to copy the function and call it the way mentioned, lovely...............

Thanks..........
Posted: 2007-10-15 09:02:17 by jukimv1986
Excellent code!!!
I had been searching how to do it, and I must admit yours is the best option.
Great work! THANKS!
Posted: 2007-10-16 13:32:00 by Borewicz
Thanks for that code. Keep up wydda good work!
Posted: 2007-10-31 08:41:27 by Liam
Hi! This appears to be a good piece of work, but what I want to have it do, is when the page loads, change the location of an iFrame (id=I1) to whatever is specified in the '?url=' bit. I have no idea how to do this, as I am a VB programmer.

If you can help, I would very much appreciate it.
Posted: 2007-11-04 20:55:55 by Medina
This is a nice piece of coding.

My difficulty is that "windows.location.href" is NOT pulling the entire URL with the environment where I'm working. The parameters are getting stripped off with I capture that value.

Suggestions?
Posted: 2007-11-05 07:48:46 by Yoseph
great, take five!!!
Posted: 2007-11-14 11:26:03 by Shalini
This is awesome!! Very useful information. Saved me lots of time. Thank you :)
Posted: 2007-11-16 08:05:37 by Hallgeir
Great function :-)
It saved me an hour of work.
Thanks
Posted: 2007-12-01 22:18:52 by AllHAppy
I'm already using it ar www.AllHappyDates.com!
Thanks!
Posted: 2007-12-04 13:42:02 by Evan
Thanks a bunch. Awesome function.
Posted: 2007-12-11 14:49:05 by steve
Am I missing something? Why is the 1st line needed? I didn't think you could use [ or ] in a URL. Can someone please explain? Thanks.
Posted: 2007-12-11 15:20:39 by lobo235 - Netlobo Staff Member
There are times when the square brackets are used to represent arrays of data in a URL. For example, PHP (which is arguably the most popular programming language for the web) accepts parameters with the square brackets to denote an array of data. Here is some additional information on the subject:

http://us2.php.net/manual/en/faq.html.php#faq.html.select-multiple
Posted: 2007-12-12 12:18:47 by steve
I see. So you can use them in a URL. Such as this: example.html?test[]=1&test[]=2&test[]=val3. Thanks for clearing that up.
Posted: 2007-12-12 12:19:03 by lobo235 - Netlobo Staff Member
Yup, you got it!
Posted: 2008-01-15 10:28:21 by John
This certainly managed to answer some problems I'd been having, but does anyone know how to pass a complex url via a url? e.g. I want to pass a url in to a page via the page url to dynamically populate an iframe. The url I want to pass in is something like http://www.mytestsite.com?search=articles&avd=recent and my web site url is say http://www.mysite.com. So the full url would be http://www.mysite.com?page=http://www.mytestsite.com?search=articles&avd=recent

The problem is that the whole thing gets confused by multiple question marks etc, and it all falls over in a big heap. I end up with a 404 error in teh iframe. Is there some way please of encoding the string that's being passed, or something similar, so as to avoid this problem and get the correct url parameter back?
Posted: 2008-01-15 10:36:27 by lobo235 - Netlobo Staff Member
There should be a way to encode/escape the URL for use in another URL in pretty much any programming language. In PHP you would use urlencode( ) to encode the URL and urldecode( ) to reverse the encoding. In JavaScript you would use encodeURI( ) to encode and decodeURI( ) to decode. For more info on how to escape strings in JavaScript visit http://xkr.us/articles/javascript/encode-compare/
Posted: 2008-01-16 13:04:26 by Prema
Thanks very much for making your code available
Works like a charm!
Posted: 2008-01-22 07:07:02 by CemKalyoncu
Thanks, I needed to parse the parameters sent after the "#", I use anchor since I dont want to reload the page but when the page is reloaded manually I want it to stay the same page in the list (using ajax to show the list). Also for the url decoding you may look at
http://www.webtoolkit.info/
Posted: 2008-01-30 15:25:53 by lilroo
Great code. Do we need to credit you in the code at all if used as is?
Posted: 2008-01-30 15:27:15 by lobo235 - Netlobo Staff Member
A credit in the code (such as the url for the article) would be nice but it is not necessary. Thanks for stopping by the site!
Posted: 2008-03-01 08:49:23 by Andrew
Works a treat...BUT I'd like to see strings with "&" in them, i.e. hb.htm?n1=Andrew&n2=David, Larke & Jane&n3=John
My regex knowledge is not good enough to cope, can you help me with this?
Posted: 2008-03-07 10:36:09 by Silas
I had to re-write it cause I need the anchor variable. This will output the anchor variable.

gup = function(){
var regexS = "([\\#][^]*)";
var regex = new RegExp(regexS);
var results = regex.exec( window.location.href );
if(results == null) return "";
else return results[0].replace("#","");
}

alert(gup());
Posted: 2008-03-08 16:12:42 by raafat
excellent code....works great for me! thanks a bunch!
Posted: 2008-03-19 12:56:03 by aldo
Many tnx!! Really useful :-)
Posted: 2008-04-17 07:13:58 by sameera
very good article.. keep up wid this type of good work!! (y)
Posted: 2008-04-24 06:58:59 by Ginchen
Thanks, exactly what I was looking for! :)
Posted: 2008-05-01 13:46:07 by Derek
This is exactly what I need for a project I am working on, however I need to append the result to another script... ie

<script language="JavaScript"><!--

/* You may give each page an identifying name, server, and channel on
the next lines. */

s.pageName="Name - " + Date() + " source = " frank_param

since I'm an absolute newbie to this I'm at a loss as how to add the result to the s.pageName

Any help would be greatly appreciated.
s.server=""
s.channel=""

Posted: 2008-05-19 09:44:33 by dennyhalim
i need one that works when i call my js like this:
http://my.website/myscript.js?var1=2&var2=3

i see this function use window.location.href
so i think it wont works.

anything else i can use??
tia
Posted: 2008-05-30 07:49:32 by Jon
Great function! Does the trick for me! Thank you!
Posted: 2008-06-10 12:37:18 by Roger
Many thanks!

I was going to write the same code, but your has given me this time :-)
Posted: 2008-06-16 08:55:13 by kai
hey lobo235,
thanks for the script. I am using it for Joomla and it works well for normal URLs without SEF turned on, i.e. index.php?option=com_contact&task=view&contact_id=1&Itemid=101

however, once SEF is turned on, the URL becomes something like option,com_contact/task,view/contact_id,1/Itemid,101

The & changes to / and = changes to ,

The ? also changes to /

How do I parse the URL in this case?

Moreover, is it possible for the function to automatically detect which type of URLs are present?

Posted: 2008-06-27 09:57:23 by davgallmty
Very cool function, it helps me a lot. Thanks!
Posted: 2008-06-27 12:59:46 by bl
Hay,
This was exactly what I was looking for.

great work. Thankx mate.

bl
Posted: 2008-07-14 07:46:20 by Dielman
Possibly something easy to answer, but I'm self taught up to this point.

I've got a site that pulls an xml feed of a users Gamertag data dependent on a value in the url, that works fine. It saves that xml to a local server to later (in the page) pull the data in javascript to display it.

If I have the xmlDoc.load load a static item (eg. gamertag.xml) it works fine, but I want it to load dependent on the same url variable I use to pull the xml feed.

I've used the above to get the variable, the page recognises it (a document.write(tag_param); confirms this), but I've got no idea of how to format the xmlDoc.load to use the variable.

Feeds are saved to feeds/gamertag.xml relative to the site. Any help?

xmlDoc.load("feeds/"tag_param".xml"); was my first thoughts... But doesn't work.
Posted: 2008-07-14 07:47:54 by lobo235 - Netlobo Staff Member
Dielman,

Your idea to use the following is close:
xmlDoc.load("feeds/"tag_param".xml");

You need to change it to:
xmlDoc.load("feeds/"+tag_param+".xml");

That should do it for you.
Posted: 2008-07-21 09:11:09 by Dielman
Another follow up question.

I got the above to work, but have problems when a space or a %20 is included in the url. It isn't recognised by the script and then used.
Posted: 2008-07-24 08:36:20 by Geetanjali
This has been great help. Good job :)
Posted: 2008-08-19 09:31:23 by Dielman
Can anyone provide me with some assistance?

I've got this:

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

var xmlDoc=null;
var tag_param = gup( 'gamertag' );

Which works, and I can verify it with

document.write(tag_param);

But for some reason when I use:

xmlDoc.load("feeds/"+tag_param+".xml");

It doesn't work, unless there's no spaces or %20's in the original URL value.

This has been driving me mad for 3 weeks, could really use some help here.

RSS Feed Icon Subscribe to the comments for this article

Post your comment for the Get URL Parameters Using Javascript article:

Name (required) (letters and numbers only):
Email (required) (will not be published):
Website (include http://):
Comment (required): (HTML tags allowed: pre, strong, em, b, i)