User:Mike Dillon/Scripts/params.js

This is an old revision of this page, as edited by Mike Dillon (talk | contribs) at 03:03, 7 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> */

var getParameterMap = (function () {
    var urlParams = {};
    return function (url) {
        if (!url) url = ___location.href;
        if (urlParams[url]) return urlParams[url];

        var params = {};
        var search = url.split("?")[1];
        if (search) {
            // Split query string on "&"
            var kvs = search.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);
            }
        }

        urlParams[url] = params;
        return params;
    };
})();

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

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

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

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