Skip to Content

getQueryStrings()

Get the query strings from the URL using JavaScript.

JavaScript July 2, 2019

Usage

JavaScript
nebula.getQueryStrings(url, format)

Parameters

url
(Optional) (String) The URL to parse for query parameters
Default: document.URL

format
(Optional) (String) The format to return the query string in
Default: object

Parameter Notes

Using string for the format will return the full query string as a single string (including the ?).

Request or provide clarification »

Source File

Located in /assets/js/nebula.js on line 3791.

JavaScript
nebula.getQueryStrings = function(url, format){
    if ( !url ){
        url = document.URL;
    }

    if ( !format ){
        format = 'object';
    }

    var queryString = url.split('?')[1];

    if ( queryString ){
        if ( format === 'string' ){
            return '?' + queryString;
        }

        var queries = {};
        var queryStrings = queryString.split('&');
        for ( var i = 0; i < queryStrings.length; i++ ){
            var hash = queryStrings[i].split('=');
            if ( hash[1] ){
                queries[hash[0]] = hash[1];
            } else {
                queries[hash[0]] = true;
            }
        }

        return queries;
    }

    if ( format === 'string' ){
        return '';
    }

    return false;
};

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
nebula.getQueryStrings = function(url, format){
    //Write your own code here, leave it blank, or return false.
}