Make your dreams come true love what you do.

Get URL Variables

Last updated on:

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

Usage

Example URL:

http://www.example.com/index.php?id=1&image=awesome.jpg

Calling getQueryVariable("id") - would return "1".
Calling getQueryVariable("image") - would return "awesome.jpg".

Comments

  1. kidi
    Permalink to comment#

    Requests = {
    QueryString : function(item){
    var svalue = location.search.match(new RegExp(“[\?\&]” + item + “=([^\&]*)(\&?)”,”i”));
    return svalue ? svalue[1] : svalue;
    }
    }

    //usage
    Requests.QueryString(“id”);

  2. sghakgun
    Permalink to comment#

    nice ! thanks.

  3. Permalink to comment#

    Great work, this is a really nice bit of coding! As always, keep it up!

  4. Nice, really helpful thanks!!

  5. Thanks Chris, totally useful script. Soo going to use that today :D

  6. anup
    Permalink to comment#

    beauty!!!!!!!

  7. Uzair
    Permalink to comment#

    Can i remove the .html extension from my website having a windows based hosting?

    I dont know if the server has Isapi_rewrite installed

    regards

  8. i think that line = return (false) would be create a bug if your url get more than one variable. false can be break a loop and return just first var in your url..

    i think that line should be remove..

    regards,

    nb : pardon me if i wrong :)

    • Patrick McDougle
      Permalink to comment#

      I think you might be mis-reading the brackets. After the for loop has searched all of the vars (name-value pairs) and hasn’t found variable then this function returns false.

  9. Neil
    Permalink to comment#

    Awesome! Thanks very much!

  10. hi my friends sorry if i speak english but i can’t explain in french so
    i got this one in one scripte i created it
    page.html?nom=28/09/1986&from=hossa&too=hisok
    and i want to get the name of variable nom and from and too
    i tried many scripts but they don’t work for me
    please can you tell me how to do it in javascript
    thanks so much

    • Patrick McDougle
      Permalink to comment#

      I think that is what this post is about.

      Just call the function defined above.

      getQueryVariable("nom"); – would return “28/09/1986″.
      getQueryVariable("from"); – would return “hossa”.
      getQueryVariable("too"); – would return “hisok”.

  11. thanks to share :D

  12. Permalink to comment#

    I tested the function of various types and only this worked.
    Congratulations!

  13. I wanted to use this in CoffeeScript if you want to copy and paste this worked for me

    getQueryVariable = (variable) ->
    query = window.location.search.substring(1)

    for el in query.split "&"
                pair = el.split "="
        return pair[1] if pair[0] is variable
    
    return false
    

    tabs is all screwed up

Leave a Comment

Use markdown or basic HTML and be nice.