User:Mike Dillon/Scripts/params.js

/* <pre><nowiki> */

// function getParameterMap(url): Parses a URL and extracts the query string parameters;
//     if no URL is provided, uses the current URL. Caches parsed parameters between invocations.
var getParameterMap = (function () {
    // URL parameters cache: key=url, value=paramMap
    var urlParamMaps = {};

    return function (url) {
        // If no URL is passed in, use the current page's URL
        if (!url) {
            url = location.href;
        }

        // If the parameters for this URL have already been parsed, return them
        if (urlParamMaps[url]) return urlParamMaps[url];

        // Set up a new map for the parameters to be parsed
        var paramMap = {};

        // Split the URL from the query string
        var search = url.split("?", 2)[1];
        if (search) {
            // Split query string on "&"
            var kvs = search.split("&");
            for (var i in kvs) {
                // Split each key-value pair on the equals sign
                var kv = kvs[i].split("=", 2);
                var key = kv[0];
                var value = decodeURIComponent(kv[1]);

                // On the first occurence of a key, seed an empty array into paramMap
                if (!paramMap[key]) {
                    paramMap[key] = [];
                }

                // Push the new value onto the value list for the key in paramMap
                paramMap[key].push(value);
            }
        }

        // Cache the paramMap to avoid parsing for all parameter requests
        urlParamMaps[url] = paramMap;

        return paramMap;
    };
})();

// function getParameterValues(key, url): Extracts the list of values for a particular key
//     from the given URL; if no URL is provided, uses the current URL.
//     Returns null if the parameter was not in the URL.
function getParameterValues(key, url) {
    return getParameterMap(url)[key];
}

// function getParameterValues(key, url): Extracts the values for a particular key
//     from the given URL; if no URL is provided, uses the current URL.
//     If there is more than one value for the given key, the first value is returned.
//     Returns null if the parameter was not in the URL.
function getParameter(key, url) {
    var values = getParameterValues(key, url);
    return values ? values[0] : null;
}

// function getParameterNames(url): Extracts the list of parameter names
//     from the given URL; if no URL is provided, uses the current URL.
function getParameterNames(url) {
    var names = [];
    for (var n in getParameterMap(url)) {
        names.push(n);
    }
    return names;
}

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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.