Tom Tom - 4 months ago 29
Javascript Question

jquery get querystring from URL

Possible Duplicate:

How can I get query string values?

I have the following URL:

What I need is to get the value of
from the URL into a variable and then use it in a jQuery code:

var thequerystring = "getthequerystringhere"

$('html,body').animate({scrollTop: $("div#" + thequerystring).offset().top}, 500);

Does anyone know how to grab that value using JavaScript or jQuery?



This is what you need :)

The following code will return a JavaScript Object containing the URL parameters:

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
        hash = hashes[i].split('=');
        vars[hash[0]] = hash[1];
    return vars;

For example, if you have the URL:

This code will return:

    "me"    : "myValue",
    "name2" : "SomeOtherValue"

and you can do:

var me = getUrlVars()["me"];
var name2 = getUrlVars()["name2"];