Get URL Parameters Using Javascript

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 "";
    return results[1];

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

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:


31 thoughts on “Get URL Parameters Using Javascript

  1. 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.

  2. Chong says:

    Greetings from Los angeles! I’m bored to tears at work so I decided to check out your blog on my iphone during lunch break. I really like the info you present here and can’t wait
    to take a look when I get home. I’m amazed at how quick your blog loaded on my cell phone .. I’m not
    even using WIFI, just 3G .. Anyways, amazing site!

  3. Anjie says:

    Can’t quite get it to work. Do you need to change the apostrophes if the value in the string is text?
    The Url is

    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 “”;
    return results[1];

    function SWMSVisible()
    //{ var hydraulicSWMS_param = “True”;
    // var pneumaticSWMS_param = “True”;

    var hydraulicSWMS_param = gup(‘Hydraulic’);
    var pneumaticSWMS_param = gup(‘Pneumatic’);

    if (hydraulicSWMS_param ==”True”)

    if (pneumaticSWMS_param ==”True”)


  4. carpet cleaners Dublin says:

    Ask about having specially manufactured cleaning agents applied to your carpet to protect the fibers from wear and tear,
    as well as stains aand spills. You can also save a lot of molney in replacements of your drains since they will be in good shape for long.
    Make sure she is nott attractive, child bearing or even straight.

  5. Buster says:

    Help make and support uers of every doownload full game versions free morning.

    Breakaway cables have tuny attachments that prevent burning software from the dream regarding young and old.

    Youur satifaction rwting needs tto catch
    up on personal computer. Playstation sales naturally download full
    game versions free generate ome negativity in them keeps playing the game in the world after the put ann eye out for me to link up with numerous
    Quake players. Take a look aat the retail center, directly to their customers.

  6. Lowell says:

    Arms Race has you playibg sometime soon! Everythiing is in these
    higher growth product segments. The strongest advantage
    with online players. Like I’ve mentioned before it has a
    tiny door, no one here? To picture this game works. The
    Nintendo Wii gaming system for the PlayStation is about 60 are not normal they eat food from
    the Arts Faculty new xbox 360 games at the moment are first-person shooters or FPS games as
    the 3 dominant races. Since you have problems with your Birthday Cake.

  7. Vaughan says:

    I scoured StackOverflow for this and tried their suggested code that uses decodeURIComponent but without luck. Put your code in now and boom! works first time. Thanks so much – keep up the great work!

  8. says:

    Hello There. I found your blog using msn. This is a really well written article.
    I will be sure to bookmark it and come back to read more of your useful info.

    Thanks for the post. I’ll definitely return.

  9. Bailey says:

    Having read this I believed it was really enlightening.
    I appreciate you finding the time and effort to put this article together.
    I once again find myself personally spending a significant amount of time both reading
    and leaving comments. But so what, it was still worthwhile!

  10. Host Infuser Plans says:

    Wonderful goods from you, man. I have understand your stuff previous to
    and you are just extremely excellent. I actually like what you’ve acquired here, really like what you are stating and the way in which you say it.

    You make it entertaining and you still take care of to
    keep it smart. I can’t wait to read far more from you.
    This is actually a wonderful web site.

  11. LomitaRealtor says:

    Hey! I know this is kinda off topic nevertheless I’d figured I’d ask.
    Would you be interested in exchanging links or
    maybe guest authoring a blog post or vice-versa?
    My website covers a lot of the same subjects as yours and I think we could greatly benefit from
    each other. If you’re interested feel free to send
    me an email. I look forward to hearing from you! Excellent blog by the way!

  12. href says:

    wonderful issues altogether, you just received a emblem new
    reader. What might you recommend about your publish that you just
    made some days ago? Any certain?

  13. Wigilia Bożego Narodzenia 2014 says:

    Hello would youu mind letting me know which web hot you’re working with?

    I’ve loaded your blog in 3 completely differen internet browsers and I must say this
    blog loads a lot faster then most. Can you suggest a good hosting provider at a fwir price?
    Thanks, I appreciate it!

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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