User:Mike Dillon/Scripts/params.js

This is an old revision of this page, as edited by Mike Dillon (talk | contribs) at 22:17, 6 January 2007. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* <pre><nowiki> */

// Get all URL parameters from query string
var getParameterMap = (function () {
    var params = {};
    if (___location.search) {
        // Trim the initial "?" and split on "&"
        var kvs = ___location.search.replace(/^\?/, "").split("&");
        for (var i in kvs) {
            var kv = kvs[i].split("=", 2);

            var key = kv[0];
            var value = unescape(kv[1]);

            if (!params[key]) {
                params[key] = [];
            }

            params[key].push(value);
        }
    }

    return function () { return params };
})();

function getParameterValues(key) {
    return getParameterMap()[key];
}

function getParameter(key) {
    var values = getParameterValues(key);
    return values ? values[0] : null;
}

function getParameterNames() {
    var names = [];
    for (var n in getParameterMap()) {
        names.push(n);
    }
    return names;
}

/* </nowiki></pre> */