Jigs Jigs - 7 months ago 19
Javascript Question

extract GET parameters from a user inputed url with javascript

I am looking to use javascript to extract the GET parameters from a user inputed url.

For example is a user enters a url say:

http://www.youtube.com/watch?v=ee925OTFBCA

I could get the v parameter

'ee925OTFBCA' as a variable

Thanks in Advance.

Answer

This should do the trick

// include this somewhere available 
var Query = (function(){
    var query = {}, pair, search = location.search.substring(1).split("&"), i = search.length;
    while (i--) {
        pair = search[i].split("=");
        query[pair[0]] = decodeURIComponent(pair[1]);
    }
    return query;
})();


var v= Query["v"]

This only runs its computation once and creates an object with name/value pairs corresponding to those supplied as parameters