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>

Related posts:

  1. Alternate Table Row Background Using JQuery Alternating your table row background color is easy to do...
  2. Reverse The WordPress Comments From Newest To Oldest If you want to display your comment list from newest...
  3. Want EDGE on your T Mobile iPhone? If you have the iPhone 3G, 3Gs or 4G, you...
  4. How To Remove Categories From Your URL WordPress is my favorite blogging platform but it’s not perfect...
  5. Key Elements of a Good Website There are several factors that go into making a successful...

11 Comments for "Retrieve URL Query String with JQuery"

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

    Reply To This Comment

    UK Managed Dedicated Servers   —   March 29, 2013
  2. NiCe PoSt sir can i have insert method with jQ ?! thanks in advance beautiful summer dresses

    Reply To This Comment

    Amaan   —   March 19, 2013
  3. really good information!!!

    Reply To This Comment

    PMG India   —   March 6, 2013
  4. i like your site. i hope all in person like your site.your site is very sweet.i love your site.

    bd jobs

    Reply To This Comment

    bonjasan   —   February 19, 2013
  5. Thank you for sharing valuable information. Nice article. I enjoyed reading this article.
    part time job

    Reply To This Comment

    part time job   —   February 7, 2013
  6. Laptop Battery High Quality Replacement HP Laptop Batteries Compaq Laptop Batteries in UK

    Reply To This Comment

    maxjisweb   —   December 28, 2012
  7. 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

    Reply To This Comment

    Bill Smith   —   September 27, 2012
  8. 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 .

    Reply To This Comment

    dating support   —   September 6, 2012
  9. 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?

    Reply To This Comment

    Tim   —   May 18, 2012
  10. 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?

    Reply To This Comment

    Jon B   Replied:

    What programming language is the page written in?

    Reply To This Comment

    Tim   —   May 18, 2012
  11. Nice bit of code, helped me out on a current project, thanks!

    Reply To This Comment

    Jon B   Replied:

    No problem, good to hear that man.

    Reply To This Comment

    Ben Sky   —   March 22, 2012

Leave a Reply





Connect With Me

Check out my about.me profile!

Latest Testimonial

"Jon is very professional; quick response to all questions and emails, very clear communication, and excellent work with very quick turnaround..."
[ ...read more... ]

Matt — 3/6/12