A simple and flexible JavaScript library that manages "window.location.hash". With the ability to support queries. (#value?query)
This project is maintained by irmmr
These are the old methods that were used in hash.js
and are now deprecated. However, you can still access them.
These functions are used to set the page hash.
value -> Adjust the value you want.
- return -> boolean
setValue
differs from the set
in that the set
clears everything to record the new value, but setValue
retains the queries.
Hash.setValue('hello-val');
Page's hash => '#hello-val'
queries -> List of all queries to set.
- return -> boolean
setQuery
does the exact opposite of setValue
and only deletes queries to register new values and leaves the values unchanged.
Hash.setQuery({
page: 2,
sort: 'desc'
});
Page's hash => '#?page=2&sort=desc'
These functions are for receiving hash values and page queries.
For examples, we consider page hash :
#hello-val?page=2&sort=desc&online&emp=
- return -> string, the hash value
Hash.getValue();
Returns => 'hello-val'
This function can be selected and used in several ways. The value of queries is defined as follows:
This item is used to receive all queries.
- return -> object, list of all queries
Hash.getQuery();
Returns => Object {page: "2", sort: "desc", online: null, emp: ""}
This item is used to receive all queries.
query -> The query name as string.
- return -> string|null|undefined, the query value
Hash.getQuery('sort'); // Returns => 'desc'
Hash.getQuery('online'); // Returns => null
Hash.getQuery('name'); // Returns => undefined
Receive the value of two or more queries.
queries -> The queries name list.
- return -> object, the queries list
If you request a query that does not exist in the query name list, the undefined value will be returned so that there is no error in executing your code.
1)
Hash.getQuery(['sort', 'online']);
Returns => Object {sort: "desc", online: null}
2)
Hash.getQuery(['sort', 'online', 'nova', '_ha']);
Returns => Object {sort: "desc", online: null, nova: undefined, _ha: undefined}
These functions are for adding values or queries to the page hash.
For examples, we consider page hash :
#/message?name=joe&message=hi
value -> The value string that you want to add on hash.
- return -> boolean
Hash.addValue('Me');
Page's hash => '#/messageMe?name=joe&message=hi'
queries -> The list of queries.
- return -> boolean
Hash.addQuery({
pri: 0,
comment: 'test-message'
});
Page's hash => '#/message?name=joe&message=hi&pri=0&comment=test-message'
These functions are for removing values or queries from the page hash.
For examples, we consider page hash :
#/message?name=joe&message=hi
values -> The value/values to remove from page hash value.
- return -> boolean
Hash.removeValue('message');
Page's hash => '#/?name=joe&message=hi'
values -> The query/queries name to remove from page hash query.
- return -> boolean
Hash.removeQuery('name');
Page's hash => '#/message?message=hi'
This function is for updating queries.
For examples, we consider page hash :
#nova?co=true&opp=rare
queryName -> The query name for update.
value -> The new value.
- return -> boolean
Hash.updateQuery('opp', 'lool');
Page's hash => '#nova?co=true&opp=lool'
These functions are for checking the existence of quantities and queries.
For examples, we consider page hash :
#the-value?page=1&name=uui
values -> The values and strings to check.
- return -> boolean, true if all values exist and false if not all values are available.
Hash.haveValue('the-'); // Returns => true
Hash.haveValue(['value', '-']); // Returns => true
Hash.haveValue('/val'); // Returns => false
queries -> The queries name to check.
- return -> boolean, true if query/queries exist and false if query is not exists.
Hash.haveQuery('page'); // Returns => true
Hash.haveQuery(['page', 'mes']); // Returns => false
Hash.haveQuery('dir'); // Returns => false
These functions are for checking values and estimates.
For examples, we consider page hash :
#my-val?page=3&redirect=/home
value -> The string to check.
- return -> boolean, true if it’s same and false for not same.
Hash.isValue('myValue'); // Returns => false
Hash.isValue('my-val'); // Returns => true
queryName -> The name of query to check.
value -> The value of query to check.
- return -> boolean, true if query has same valued and false for different value.
Hash.isQuery('page', '5'); // Returns => false
Hash.isQuery('redirect', '/home'); // Returns => true
Hash.isQuery('message', undefined); // Returns => true
These functions are for clearing hashes and their values completely.
For examples, we consider page hash :
#val?page=1
- return -> boolean
Hash.clearValue();
Page's hash => '#?page=1'
- return -> boolean
Hash.clearQuery();
Page's hash => '#val'