Question

Hello I really want to learn how to do something like this. If you go to a page for example it says http://example.net/search.html?catagory=food&includelevelone=true. I do not have access to php so it can only be HTML and Javascript/jQuery. Thanks in advance!

Was it helpful?

Solution

The part of the URL that is from the questionmark onwards is called a query string.

Here is a pure JavaScript function to parse the query string to obtain particular values:

function querystring(key)
{
    var filter;
    var value;

    key     = key.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
    filter  = new RegExp('[\\?&]' + key + '=([^&#]*)');
    value   = filter.exec(window.location.search);

    if(value == null)
    {
        return '';
    }
    else
    {
        return decodeURIComponent(value[1].replace(/\+/g, ' '));
    }
}

You just pass in the query string key name you're interested in (as a string) and you get the value back (also as a string.) An example of how you could use the function:

alert('Category = ' + querystring('catagory'));

OTHER TIPS

Everything behind the questionmark is a url parameter. every word left of an equal sign is the name of the parameter, everything right of an equal sign is the corresponding value. The name-value-pairs are divided by &-signs

Here are two pages i quickly googled that are about getting these parameters in JavaScript (wich is really not that hard):

http://code-tricks.com/get-url-parameters-using-javascript

http://ziemecki.net/content/javascript-parsing-url-parameters

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top