Set & Get Query Parameters In URL – hashparser

Category: Javascript | February 24, 2025
Author:rvanbaalen
Views Total:104 views
Official Page:Go to website
Last Update:February 24, 2025
License:MIT

Preview:

Set & Get Query Parameters In URL – hashparser

Description:

hashparser is a super tiny JavaScript library that helps you handle (set/get) query parameters in the URL.

How to use it:

1. Install and import the hashparser.

# NPM
$ npm install @rvanbaalen/hashparser
import {HashParser} from '@rvanbaalen/hashparser';

2. Or include the hashparser’s JavaScript on the page.

<script src="dist/hashparser.min.js"></script>

3. Append query parameters to the current URL.

const hp = new HashParser();
hp.set('css', 'script');
=> https://youtsite.com/page.html#css=script

4. Determine whether to encode the query parameter value.

// example.com#css=InNjcmlwdCI=
HashParser.encoded.set('css', 'script');

HashParser.setParameter(‘example’, greeting, true);

5. Get the value of a query parameter.

// script
hp.get('css'); 
// script
HashParser.encoded.get('css');

6. Remove the query parameter.

hp.remove('css');

7. Available options:

const hp = new HashParser({
  // set to true to always encode values
  encoded: false, 
  // listen for hashchange events on the window object and update the internal dataset.
  sync: true,
  
});

You Might Be Interested In:


Leave a Reply