Hash.js - We Stand Behind #

Hash.js is a micro (0.5 KB) JavaScript lib that makes it easy to manipulate everything that is behind # in the url.

Check out the Github repo at github.com/javve/hash.js and see it in action at PNGTextures.com

Notice: The main focus of Hash.js is simplcity. This is no tool for your super-ajax-native-feeling-app-with-google-bot-support. If that’s your usage situation, use the awesome History.js.

Usage

Just include hash.min.js or hash.js at your site and then a hash object will become available.

1
2
3
4
5
<script src="hash.min.js"></script>

<script>
    hash.add({foo: "bar" });        // Url becomes http://url.com#foo=bar
</scrtip>

Methods

  • hash.add(params)
    Add parameter to hash
1
2
3
4
5
hash.add({ foo: "bar" });
// http://url.com#foo=bar

hash.add({ car: "dar", sar: "par" });
// http://url.com#foo=bar&car=dar&sar=par
  • hash.get(param) or hash.get()
    Returns value of paramter in hash. If param is undefined then all values are returned.
1
2
3
4
5
var fooValue = hash.get('foo');
// fooValue == "bar"

var allValues = hash.get();
// allValues == { foo: "bar", car: "dar", sar: "par"}
  • hash.remove(param)
    Removes the value with name param.
1
2
hash.remove('foo');
// http://url.com#car=dar&sar=par
  • hash.clear()
    Clears entire hash.
1
2
hash.clear();
// http://url.com#

Example

About the author and discussions

I'm Jonny. A Designer, Entrepreneur & JavaScript Hacker from Stockholm. Currently designer at Mynewsdesk. Behind List.js, Teamtailor, Stockholm Startup Hack, SilarApp and StartupLocation.

If you want to comment, discuss, upvote or downvote this post, Hacker News is the place to be.