Javascript, More Query Strings

javascript.pngAs I mentioned in my last post regarding Javascript and Query strings, the use of the search property returning everything that comes after the question mark(?) will be used here to retrieve the data we need from the query.

For example by obtaining the document.location objects chained with the search property and the substring() string function on the following query string we will only return what is after the question mark, since that's all we need.

The Query string:

    http://www.2whoa.com/dominate/index.php?name=2whoa&user=puck
        &item=javascript


The code and results:

    var query =  document.location.search.substring(1);

    alert(query); // results , "name=2whoa&user=puck&item=javascript"


That's useful and good to know. Now what?
I'm going to write this into a function that will allow the ability of obtaining the results from the query strings when ever we need to do so. i am also going to pass a string(searchString) into the function which will be the value, parameter, or variable we looking to retrieve from the Query.

    function queryString(searchString) {

       var query = document.location.search.substring(1);

       alert(query);

    }


Now we can place this function in a global script so it will always be accessible. But, before you do that, there's still  more work to do to complete the function.

Let's introduce the split() method into the queryString function and break the string into an array. Which will allow us to iterate through a for loop and now split the items array on the equal sign(=).

    var items = query.split("&");

    for (var i = 0; i < items.length; i++) {

       var item = items[i].split("=");

       alert(item[0] + " : " + item[1]);
    }


Right about now you should be getting alerts for all the names and values that were contained in the query string. Which means if your using my example, you should be getting 3 alerts, after you place the previous code into the function queryString();. What happened was that we ran through the for loop and created 3 new arrays which over wrote the previous array as we iterated through the loop, leaving us with only values at item[0] and item[1].

The function should appear like this now:

    function queryString(searchString) {

       var query = document.location.search.substring(1);

        var items = query.split("&");

            for (var i = 0; i < items.length; i++) {

               var item = items[i].split("=");

               alert(item[0] + " : " + item[1]);

            }
    }


As I mentioned, the function is being passed a value to search the query string for, so we need to added an if statement to check for the searchString.

    if (item[0] = searchString) {

       alert(item[1]);
    }


To make this more useful replace the alert with a return so you are actually retrieving the searchString from the original call to the function.

Here is the completed function and the initial call made to it.

    function queryString(searchString) {

       var query = document.location.search.substring(1);

       var found = "";

       var items = query.split("&");

            for (var i = 0; i < items.length; i++) {

               var item = items[i].split("=");

               if (item[0] == searchString) {

                        var found = item[1];
                        return found;
               }
           }
      
          return "not found";
    }

    var item = queryString("item");
   
    alert(item); // Displays the result "javascript"


That's all that's to it. The split() method can be a vary useful to use on all strings that get returned from many different sources.


Leave a comment

BMW, Toyota, BMW, Saab, Audi, Nissan Mercedes, BMW, Nissan, Honda,bmw bmw bmw Acura, Honda, Acura Forerunner, SUV, BMW, Mercedes, Toyota  
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota


Chicago Blackhawks
Chicago Blackhawks
Stanley Cup
Stanley Cup
Pittsburgh Penguins
Pittsburgh Penguins
Boston Bruins
Boston Bruins
Fire & Flames
Fire & Flames








 


Categories

BMW, Toyota, BMW, Saab, Audi, Nissan Mercedes, BMW, Nissan, Honda, Acura, Honda, Acura Forerunner,bmw bmw bmw SUV, BMW, Mercedes, Toyota