Locksmack

Technology, Web Development, Philosophy and more…

Get Query String with JavaScript

One of the principle things I recall wanting to do as a novice web developer was to access the query string of a URL. I wanted to do this for many reasons, so after some googling I discovered the perfect function for my needs. I found it over at ilovethecode.com

Here it is:

 function querySt(ji) {
     hu = window.location.search.substring(1);
     gy = hu.split("&");
     for (i=0;i<gy.length;i++) {
         ft = gy[i].split("=");
         if (ft[0] == ji) {
             return ft[1];
         }
     }
 }

As easy as that. All you need to do is call this function which will return the value of any parameters in the query string. Lets say we had a url like:

http://www.locksmack.com?debug=true

Getting the value of the ‘debug’ parameter is as simple as doing:

var debug = querySt("debug");

OR

if(querySt("debug") == "true"){
    //blah blah
}

Simple! Just remember that the value returned is a string, so it is the word ‘true’ and not the boolean value. This function works with multiple parameters in a query string, separated by ampersands (&). Give it a try and let me know how you go.

Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Switch to our mobile site