www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Query string of the URL

  1. #1
    Join Date
    May 2008
    Posts
    55

    resolved [RESOLVED] Query string of the URL

    I have the code for querying string of the URL.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
        <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"
    
        </script>
    </head>
    <body>
    
    </body>
    </html>
    I am not fully understanding it.
    Assume the query string
    Code:
    ?q=javascript&num=10
    What is qs and items in the code?

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    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

    http://www.webdeveloper.com/forum/editpost.php?p=1306301&do=editpost

    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 12:07 PM.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,154
    Why not turn the query string if any in to an object?

    Code:
    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 05:24 PM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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



Recent Articles