This is how you can get URL of current page:
var url = window.location;
var urlString = window.location.href;
This is how you can parse this string to get an object with key-value pairs representing query parameters:
function parseQuery(url) {
var result = {};
var parameters = window.location.search;
if (!parameters) return result;
parameters = parameters[1].split("&");
for (var parameter in parameters) {
var keyValue = parameters[parameter].split("=");
result[keyValue[0]] = decodeURIComponent(keyValue[1]);
}
return result;
}
var test = parseQuery(window.location.href);
var first = test.first;
var second = text.second;
first = test["first"];
second = text["second"];
Yes, I tested it before posting. :-)
Note that my function does not take into account all pathological cases. You can use it as it, then you may deal with
undefined
objects returned as values, and so on. Or you can add some check-ups, according to your assumptions on the query string and your requirements.
[EDIT] I improved my code sample according to the suggestions by Richard Deeming in his comments to the answer, see below.
—SA