1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Getting Query parameters in Javascript

Discussion in 'JavaScript and AJAX' started by pradeep, Dec 7, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    We sometimes require to get the query parameters of an URL in JavaScript, here's how we can do that.

    Code:
    // get the current URL
     var url = window.location.toString();
     //get the parameters
     url.match(/\?(.+)$/);
     var params = RegExp.$1;
     // split up the query string and store in an
     // associative array
     var params = params.split("&");
     var queryStringList = {};
     
     for(var i=0;i<params.length;i++)
     {
         var tmp = params[i].split("=");
         queryStringList[tmp[0]] = unescape(tmp[1]);
     }
     
     // print all querystring in key value pairs
     for(var i in queryStringList)
         document.write(i+" = "+queryStringList[i]+"<br/>");
    Example usage:
    HTML:
    <script language='JavaScript'>
     // get the current URL
     var url = window.location.toString();
     //get the parameters
     url.match(/\?(.+)$/);
     var params = RegExp.$1;
     // split up the query string and store in an
     // associative array
     var params = params.split("&");
     var queryStringList = {};
     
     for(var i=0;i<params.length;i++)
     {
     	var tmp = params[i].split("=");
     	queryStringList[tmp[0]] = unescape(tmp[1]);
     }
     
     // print all querystring in key value pairs
     for(var i in queryStringList)
     	document.write(i+" = "+queryStringList[i]+"<br/>");
     </script>
     
     
    Output:
    Code:
    ab = pradeep
     site = go4expert
     
    shabbir likes this.
  2. ankit_khandelwal16

    ankit_khandelwal16 New Member

    Joined:
    Dec 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Re: Getting Query parameters using Javascript

    Thanks Buddy
    It really helped me to solve my problem.
    Can you help me more little on this how to make any url from this to get any document of that query
     
  3. rameshb

    rameshb New Member

    Joined:
    Dec 10, 2010
    Messages:
    35
    Likes Received:
    1
    Trophy Points:
    0
    Re: Getting Query parameters using Javascript

    Thank you my friend for the post .
     
  4. ulfemsoy

    ulfemsoy New Member

    Joined:
    Oct 23, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Re: Getting Query parameters using Javascript

    Nice.

    To get a specific named argument in the query you could also do it the following way:
    lazerwire.com/2011/10/javascript-get-url-query-argument.html

    Remember to url decode the value using the "unescape" method in JavaScript.
     

Share This Page