Skip to Content
Menu

get()

Get (or check) a specific query string.

JavaScript February 7, 2021

Usage

JavaScript
nebula.get(parameter, url)

Parameters

parameter
(Optional) (String) The query parameter to look for
Default: Full query string

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

Parameter Notes

If a parameter is not passed, it returns all query strings exactly like getQueryStrings().

Request or provide clarification »

Examples

JavaScript
if ( nebula.get('foo') === 'bar' ){
    //Do something if ?foo=bar is in the URL
}
JavaScript
if ( nebula.get('foo') ){
    //Do something if ?foo or ?foo=anything is in the URL
}

Demo


Visit this page with the query string ?hello (or ?hello=whatever) and open the JavaScript console to see it.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/utilities.js on line 227.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.get = function(parameter = false, url = location.search){
        let queryParameters = new URLSearchParams(url);
    
        if ( parameter ){ //If a specific parameter is requested
            return queryParameters.get(parameter); //Return it (or null if it does not exist)
        }
    
        //Otherwise we will return all of the query parameters
        let queries = [];
        queryParameters.forEach(function(value, key){ //Do not use jQuery here!
            queries[key] = value;
        });
    
        return queries;
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript

    For non-module import functions:

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


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.get = function(parameter, url){
            //Write your own code here, leave it blank, or return false.
        }
    });


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.get === 'function' ){
            nebula.get = function(parameter, url){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });