Socket
Socket
Sign inDemoInstall

hash-handler

Package Overview
Dependencies
3
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    hash-handler

Handle location hash query strings with ease.


Version published
Weekly downloads
284
increased by2.16%
Maintainers
1
Install size
41.1 kB
Created
Weekly downloads
 

Readme

Source

hash-handler

npm Travis David

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:

// ES6
import Hash from 'hash-handler';

// CommonJS
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();

// set hash value(s)
hash.set({
  age: 5
});

// set more hash values
hash.set({
  color: 'blue'
});

// get hash query string object
console.log(hash.get()); // => { age: 5, color: 'blue' }

// replace whole hash
hash.replace({
  age: 7
});

console.log(hash.get()); // => { age: 7 }

const handler = () => {
  // do stuff
};

// listen for hash changes
hash.registerListener(handler);

// stop listening
hash.removeListener(handler);

// remove all handlers and event listener
hash.destroy();

API

Hash()

Initializes hash-handler instance.

const hash = Hash();

Hash.get()

Returns parsed query string hash.

// http://www.example.com/#name=max
console.log(hash.get()); // => { name: 'max' }

Hash.set(newHash)

Sets the hash query string. Extends existing key value pairs.

hash.set({
  name: 'max'
}); // http://example.com/#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) => {
    // called everytime hash changes
    console.log(hash); // new 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

Keywords

FAQs

Last updated on 28 Jul 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc