Get URL Query parameters using .querystring()

.querystring()


Description: Fetch a query string value and all parameters from URL.


Example:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>querystring example</title>
  <script src="http://code.bitsycode.com/bitsycode-latest.min.js"></script>
</head>
<body>
 
<strong>Single Query String</strong>
<p class="querystring"></p>

<strong>All Query Strings</strong>
<p class="querystring-object"></p>

<script>
  
    //FETCH SINGLE KEY VALUE
    bitsyCode( ".querystring" ).html( bitsyCode.querystring("plugin") );
  
    //FETCH ALL KEYS AND VALUES
    var object = "";
    var result = bitsyCode.querystring({ all:true });
    
    for (var key in result) {
        object += "  "+key + " = " + result[key] + "<br />";
    }
    
    bitsyCode( ".querystring-object" ).html("{<br />" + object + "}");  
  
</script>
 
</body>
</html>

Preview:

Single Query String

BitsyCode


All Query Strings

{
plugin: BitsyCode,
version: 1.0
}

  • Usage

    • How to use:

      .querystring() is a simple method to get query strings and URL parameters.

    • Single query string key value:

      Get a single query string key value no options.

      bitsyCode.querystring("YOUR-QUERYSTRING-KEY");
    • All query string key values:

      Get all query key values.

      bitsyCode.querystring({ all:true });
  • Options

    • What options are there:

      To get a single query string key value, you must specify the "key" value

      key: " " // Specify a query string key value.

      To get all query string key values.

      all: false (default value) // To get all query string key values, you must set "all" to "true".
  • Returns

    • Returns:

      There are 2 types of returns.

    • Single query string key:
      Return: String   // Returns a string of the query string key value.
    • All query string key values:
      Return: { }   // Returns an object of all query string values in sequence.

      Example of URL:

      http://www.bitsycode.com/?plugin=bitsycode&version=1.0

      Blue text: Those are "key" values
      Red text: Those are "value" of the keys

      Example returned object:

      Object { plugin: "bitsycode", version: "1.0" }
Share