Retrieve URL Query String with JQuery

Here’s a quick and easy way to get a query string from your URL using jquery. Suppose you have the following URL: www.myurl.com?page=mypage. Use the code below to retrieve page=mypage. Of course this can be any name/value pair…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script type="text/javascript">
  function getQuerystring(key, default_){
    if (default_==null) default_="";
    key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if(qs == null)
      return default_;
   else
     return qs[1];
 }	
 var urlString = getQuerystring('page');
 
  $(document).ready(function(){
    if ( urlString == 'mypage' ) {  
      //Do something with jquery here
    }
  });
</script>

18 Responses to “Retrieve URL Query String with JQuery”

  1. Law firms in chennai

    What programming language is the page written in?

  2. Law firms in chennai

    No problem, good to hear that man.

  3. Commercial properties chennai

    Thank you for sharing valuable information. Nice article. I enjoyed reading this article.
    part time job

  4. adelaideparker90

    Nice bit of code, helped me out on a current project, thanks!

  5. anbb

    Well, just can use my project…

  6. Rajesh

    i have slide show in one of client page it have to change images by query string. its working am using this code thanks budy

  7. UK Managed Dedicated Servers

    nice post i am in searching of this type of query thanks for this help.

  8. Amaan

    NiCe PoSt sir can i have insert method with jQ ?! thanks in advance beautiful summer dresses

  9. PMG India

    really good information!!!

  10. part time job

    Thank you for sharing valuable information. Nice article. I enjoyed reading this article.
    part time job

  11. maxjisweb

    Laptop Battery High Quality Replacement HP Laptop Batteries Compaq Laptop Batteries in UK

  12. Bill Smith

    Good share, your topic is very great and useful for us…thank you. I just like the approach you took with this subject. It isn’t every day that you discover something so concise and enlightening.
    Graphic Design Dictionary

  13. dating support

    Its really nice article .
    I will must try to follow your instruction properly. I hope your next article part-2 will published as early as possible .

  14. Jon B

    What programming language is the page written in?

  15. Tim

    I have a url query string that was set up for us previously and i am trying to get the info that string pulls to insert in a form that I have.

    Is there a simple code I can use on the registraion page to insert this info into the two fields i need it?

  16. Tim

    I have a url query string that was set up for us previously and i am trying to get the info that string pulls to insert in a form that I have.
    The URL query string is: registration?memid=300&uname=unamehere

    Is there a simple code I can use on the registraion page to insert this info into the two fields i need it?

  17. Jon B

    No problem, good to hear that man.

  18. Ben Sky

    Nice bit of code, helped me out on a current project, thanks!

Leave a Reply