smourph
3/24/2016 - 5:12 PM

Return GET parameters from URL

Return GET parameters from URL

/**
 * Return GET parameters from URL
 * @param param
 * @returns Object|string|null
 */
function $_GET(param) {
    var vars = {};

    window.location.href.replace(
            /[?&]+([^=&]+)=?([^&]*)?/gi,
            function (m, key, value) {
                vars[key] = value !== undefined ? value : '';
            }
    );

    if (vars === {} || vars.length === 0) {
        return null;
    } else if (param && (typeof param === 'string' || param instanceof String)) {
        return vars[param] ? vars[param] : null;
    }

    return vars;
}