Results 1 to 3 of 3

Thread: [RESOLVED] Query string of the URL

  1. #1
    Join Date
    May 2008

    resolved [RESOLVED] Query string of the URL

    I have the code for querying string of the URL.
    <!DOCTYPE html>
        <title>Location Example</title>
        <script type="text/javascript">
            function getQueryStringArgs(){
                //get query string without the initial ?
                var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                    //object to hold data
                    args = {},
                    //get individual items
                    items = qs.length ? qs.split("&") : [],
                    item = null,
                    name = null,
                    value = null,
                    //used in for loop
                    i = 0,
                    len = items.length;
                //assign each item onto the args object
                for (i=0; i < len; i++){
                    item = items[i].split("=");
                    name = decodeURIComponent(item[0]);
                    value = decodeURIComponent(item[1]);
                    if (name.length){
                        args[name] = value;
                return args;
            //assume query string of ?q=javascript&num=10
            var args = getQueryStringArgs();
            alert(args["q"]);     //"javascript"
            alert(args["num"]);   //"10"
    I am not fully understanding it.
    Assume the query string
    What is qs and items in the code?

  2. #2
    Join Date
    May 2006
    Somewhere behind your screen
    qs as you could have guessed stands for "query string without the initial ?" (see the comments in your code)

    items = qs.length ? qs.split("&") : []

    items = if query string has lenght then it will be an array which is the result of splitting this string by "&" or an empty array (in case the string has no length)

    for example, look at current page (this post editing page) url


    the bold part is qs
    if you split this string by "&" you'll get 2 pairs (in your code - items) which are p=1306301 and do=editpost
    every pair can also be splitted by "=" and you'll get name,value subarrays
    Last edited by Padonak; 01-08-2014 at 01:07 PM.
    signature under construction

  3. #3
    Join Date
    Mar 2007
    Why not turn the query string if any in to an object?

    if( query = location.search.slice(1) ){
    	var tmp = query.split("&");
    	delete query;
    	var query = [];
    	while( tmp.length ){
    		q = tmp.shift();
    		q = q.split("=");
    		query[ q.shift() ] = q.shift();
    }else query = false;
    if( query ) console.log(">> "+query.goodbye );
    Will output "coconuts" to the console log if the URL string was http://webdomain/some.html?hello=peanuts&goodbye=coconuts

    While it is messy, it gets the job done.
    Last edited by \\.\; 01-08-2014 at 06:24 PM.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center