Get URL Parameters Using Javascript

Posted: October 3, 2008 in 1
Tags: , , , , , , , ,

An easy way to parse the query string in your URL to grab certain values.

Get URL Parameters Using Javascript

Most of the server-side programming languages that I know of like PHP, ASP, or JSP give you easy access to parameters in the query string of a URL. Javascript does not give you easy access. With javascript you must write your own function to parse the window.location.href value to get the query string parameters you want. Here is a small function I wrote that will parse the window.location.href value and return the value for the parameter you specify. It does this using javascript’s built in regular expressions. Here is the function:

function gup( 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];
}

The way that the function is used is fairly simple. Let’s say you have the following URL:

http://www.foo.com/index.html?bob=123&frank=321&tom=213#top

You want to get the value from the frank parameter so you call the javascript function as follows:

var frank_param = gup( ‘frank’ );

Now if you look at the frank_param variable it contains the number 321. The query string was parsed by the regular expression and the value of the frank parameter was retrieved. The function is smart in a couple of ways. For example, if you have an anchor in your URL like our example URL above does (#top) the gup() function knows to stop before the # character. Also, if a requested parameter doesn’t exist in the query string then an empty string is returned instead of a null.

This function has worked very well for my query string parsing needs and should work well for you.

url source: http://www.netlobo.com/url_query_string_javascript.html

About these ads
Comments
  1. ArguemiNixCrageriVew says:

    hksbxflokpxrrswlwell, hi admin adn people nice forum indeed. how’s life? hope it’s introduce branch ;)

  2. Yohanân says:

    There is just a problem with encoded characters like space which become %20. Do you know if there is a method in javascript to decode this characters whithout making your own parser and be dependent of your server encoding (when url are generated on the server side). Thanks in advance.

  3. admin says:

    I havent think of that yet. Soon I’ll keep you posted.

  4. Shweta says:

    it was gr8..helped me alot..thanks
    saved from solving a jigsaw puzzle..

  5. Nick says:

    I still have to get my website on the web…

  6. Cesar says:

    Thanks, that was very usefull.

  7. chiponium says:

    This worked like a charm! Thanks a bunch!

  8. jmimi says:

    Tahnk you very much…

  9. Richard Bobinski says:

    Check out this example at JS Bin: http://jsbin.com/checkerboard/2 . I found it on a forum page about a new JS library called jPaq.

  10. Lars says:

    Great. I was looking for something like that the whole day. Perfect.

  11. Ali Brooke says:

    I love you.

    JUST FYI

  12. jonms83 says:

    Worked Great! Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s