Skip to content
/ hash.js Public

A simple and flexible JavaScript library that manages "window.location.hash". With the ability to support queries. (#value?query)

License

Notifications You must be signed in to change notification settings

irmmr/hash.js

Repository files navigation

Hash Js

GitHub license npm

Hash.js

It is better to use version 1.7.0 and above. Lower versions are not recommended at all.

Hash.js is a simple and flexible javascript library that manages the location.hash page. To change, add, set, check, get the hash value or query you can use this library. The page hash is a combination of 2 parts, "value" and "query". This value is set as follows: #value?query.

Install

You can install this package with ‍‍npm and use it.

npm install @irmmr/hash.js

Or use the built-in version of this library separately.

<script src="path/to/dist/hash.min.js"></script>

You can also use with: jsdelivr:

<script src="https://cdn.jsdelivr.net/npm/@irmmr/hash.js"></script>

Document

The document contains all the components included in the package.

view document ->

Test

You can test this library in the browser online. view ->

Otherwise, you must use the following commands to test the items:

# install test packages
npm run start

# test last published version
npm test

# test last changed file
npm test -- --dev

Structure

The components of this library are divided into 3 main sections, which include main, value and query. query itself includes the (str)string section so that query items can be implemented as a string.

import Hash from "@irmmr/hash.js";

// use Hash.js information
// Object { version: "1.7.5", name: "HashJs", module: "Hash" }
Hash.info();

// use Hash.js event
Hash.on(listener, (e, i) => {
  //...
});

Main: Items for general hash management. value?query

// set example
// => #string-for-me
Hash.set("string-for-me");

// replace example
// => #stringforme
Hash.replace(/-/g, "");

Value: Items for value hash management. value?query

// Hash.v == Hash.value
// set example
Hash.v.set("new-value");

// get example
console.log(Hash.value.get());

Query: Items for query hash management. value?query (objective)

// Hash.q == Hash.query
// set example
Hash.q.set("query-name", "query-val");

// get example
console.log(Hash.query.get("query-name"));

Query str: Items for query hash management as string. value?query (string)

// add example
Hash.q.str.add("q=v", "after:g");

// get example
console.log(Hash.q.str.get());

How to use?

These include a few simple examples.

Use it !

// set a value to location.hash (main component)
Hash.set("hello-babe"); // page's hash => '#hello-babe'

// get location.hash            (main component)
Hash.get(); // returns => 'hello-babe'

// set new value                (value component)
Hash.v.set("hey-value"); // page's hash => '#hey-value'

// set 'page' query             (query component)
Hash.q.set("page", 1); // page's hash => '#hey-value?page=1'

// set 'e' query                (query string component)
Hash.q.str.set("ev=12"); // page's hash => '#hey-value?ev=12'

ready() one-line usage

// #data-type?name=J
Hash.ready()?.set("data-type").q.set("name", "J");

About

A simple and flexible JavaScript library that manages "window.location.hash". With the ability to support queries. (#value?query)

Topics

Resources

License

Stars

Watchers

Forks