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>

20 Responses to “Retrieve URL Query String with JQuery”

  1. roof vent flashing repair cost

    I’ve been exploring for a bit for any high-quality articles or weblog
    posts in this sort of house . Exploring in Yahoo I ultimately stuumbled upon this website.
    Studying this information So i am glad tto convey that
    I have a veery excellent uncanny feeling Icame upon
    exactly what I needed. I such a lot certainly will make sure to don?t forget
    this site and provides it a look regularly.

  2. Roof Service

    Hello, its nice paragraph concerning media print, we all underswtand media is a
    impressive source of information.

  3. Law firms in chennai

    What programming language is the page written in?

  4. Law firms in chennai

    No problem, good to hear that man.

  5. Commercial properties chennai

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

  6. adelaideparker90

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

  7. anbb

    Well, just can use my project…

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

  9. UK Managed Dedicated Servers

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

  10. Amaan

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

  11. PMG India

    really good information!!!

  12. part time job

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

  13. maxjisweb

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

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

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

  16. Jon B

    What programming language is the page written in?

  17. 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?

  18. 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?

  19. Jon B

    No problem, good to hear that man.

  20. Ben Sky

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

Leave a Reply