hash-handler
Handle location hash query strings with ease.
Aims to make working with location hash and query strings easier. Get and set query strings as simple objects and listen for hash changes. Query string arguments get sorted to guarantee consistent URL's.
Table of contents
Install
This module is available on npm.
$ npm install hash-handler
If you are using some kind of bundler (webpack, rollup...) you can import it like this:
import Hash from 'hash-handler';
var Hash = require('hash-handler');
The UMD build is also available on unpkg:
<script src="https://unpkg.com/hash-handler/dist/hashHandler.js"></script>
Usage
import Hash from 'hash-handler';
const hash = Hash();
hash.set({
age: 5
});
hash.set({
color: 'blue'
});
console.log(hash.get());
hash.replace({
age: 7
});
console.log(hash.get());
const handler = () => {
};
hash.registerListener(handler);
hash.removeListener(handler);
hash.destroy();
API
Hash()
Initializes hash-handler instance.
const hash = Hash();
Hash.get()
Returns parsed query string hash.
console.log(hash.get());
Hash.set(newHash)
Sets the hash query string. Extends existing key value pairs.
hash.set({
name: 'max'
});
Hash.replace(newHash)
Replace existing hash query string.
hash.replace({
animal: 'shark'
});
Hash.clear()
Removes the hash fragment. The hash itself will remain.
hash.clear();
Hash.registerListener(handler)
Registers a handler to be executed on hash change.
hash.registerListener((hash) => {
console.log(hash);
});
Hash.removeListener(handler)
Removes registered listener function.
hash.removeListener(handler);
Hash.destroy()
Remove all handlers and hashchange event listener.
hash.destroy();
License
MIT © Lars Graubner