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

30 responses to “Get URL Parameters Using Javascript”

  1. ArguemiNixCrageriVew Avatar
    ArguemiNixCrageriVew

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

    Like

  2. Yohanân Avatar
    Yohanân

    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.

    Like

  3. admin Avatar

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

    Like

  4. Shweta Avatar
    Shweta

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

    Like

  5. Nick Avatar
    Nick

    I still have to get my website on the web…

    Like

  6. Cesar Avatar

    Thanks, that was very usefull.

    Like

  7. chiponium Avatar
    chiponium

    This worked like a charm! Thanks a bunch!

    Like

  8. jmimi Avatar
    jmimi

    Tahnk you very much…

    Like

  9. sinauw Avatar

    terima kasih mas

    Like

  10. Richard Bobinski Avatar
    Richard Bobinski

    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.

    Like

  11. Lars Avatar

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

    Like

  12. Ali Brooke Avatar

    I love you.

    JUST FYI

    Like

  13. jonms83 Avatar

    Worked Great! Thanks!

    Like

  14. Chong Avatar

    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!

    Like

  15. Anjie Avatar
    Anjie

    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 http://pipeline.kanddplumbing.com.au/Field/SWMSSign.aspxHydraulic=True&Pneumatic=False

    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];
    }

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

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

    if (hydraulicSWMS_param ==”True”)
    {document.getElementById(“hydraulicSWMS”).style.visibility=’visible’;}
    else
    {document.getElementById(“hydraulicSWMS”).style.visibility=’hidden’;}

    if (pneumaticSWMS_param ==”True”)
    {document.getElementById(“pneumaticSWMS”).style.visibility=’visible’;}
    else
    {document.getElementById(“pneumaticSWMS”).style.visibility=’hidden’;}

    }

    Like

  16. Ra Mänd Avatar

    If you need to get parameter to alter it, better use this function:
    http://browse-tutorials.com/snippet/alter-url-get-parameters-using-javascript

    Also a little bit more compact solution to this problem:
    http://browse-tutorials.com/snippet/get-url-variables-javascript

    Like

  17. Buster Avatar

    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.

    Like

  18. Lowell Avatar

    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.

    Like

  19. Vaughan Avatar
    Vaughan

    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!

    Like

  20. lace front wigs Avatar

    What’s up, the whole thing is going well here and ofcourse every one is sharing information, that’s
    actually good, keep up writing.

    Like

  21. Jeremiah Avatar

    Good post. I absolutely appreciate this site.
    Stick with it!

    Like

  22. inewsgr.com Avatar

    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.

    Like

  23. Bailey Avatar

    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!

    Like

  24. Host Infuser Plans Avatar

    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.

    Like

  25. yoga Avatar

    This is my first time visit at here and i am truly pleassant
    to read all at single place.

    Like

  26. LomitaRealtor Avatar

    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!

    Like

    1. rockmanx Avatar

      sorry, i did not reply sooner..

      yes, i would love to..

      Like

  27. href Avatar

    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?

    Like

  28. Wigilia Bożego Narodzenia 2014 Avatar

    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!

    Like

Leave a reply to Bailey Cancel reply

MotobiRider

Welcome to MotobiRider

Let’s connect