-1

Possible Duplicate:
Get query string values in JavaScript
Parse query string in JavaScript

http://www.example.org/search?q=example&another=test&again=more

How can I create three sepearate variables in jQuery with the values example, test, and more?

In other words, how can I extract a query from a URL based on its position (first, second, third, etc.) in the URL or based on the &another= part (not sure what it's called) of the query?

Community
  • 1
  • 1
UserIsCorrupt
  • 4,837
  • 15
  • 38
  • 41

2 Answers2

0

I use an existing plugin for tasks like these: https://github.com/allmarkedup/jQuery-URL-Parser

madflow
  • 7,718
  • 3
  • 39
  • 54
0

I use this method:

var query        = window.location.split('?')[1]; // or http://www.example.org/search?q=example&another=test&again=more
var query_obj    = unserialize(query);

function unserialize(query) {
    var pair, params = {};
    query = query.replace(/^\?/, '').split(/&/);
    for (pair in query) {
        pair = query[pair].split('=');
        params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
    }
    return params;
}

So you'll have your variables in the query_obj like this: query_obj.test

I found the function over the internet a while ago so I can't provide a link. Sorry and thanks to whoever published it first.

kugta
  • 51
  • 2