Skip to content
/ js-hash Public

A JavaScript library that keeps track of the history of changes to the hash part in the address bar.

Notifications You must be signed in to change notification settings

blixt/js-hash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Hash handler

Information

A JavaScript library that keeps track of the history of changes to the hash part in the address bar.

A callback function, "handler", is used to handle changes. The function will be called once when the library is initialized (usually when the page has finished loading) and then whenever the hash changes.

Warning for Internet Explorer 7 and below: If an element on the page has the same ID as the hash used, the history will get messed up.

Does not support history in Safari 2 and below.

Example

function handler(newHash, initial) {
    if (initial)
        alert('Hash is "' + newHash + '"');
    else
        alert('Hash changed to "' + newHash + '"');
}
Hash.init(handler);
Hash.go('abc123');

The initial argument is a boolean that indicates whether the handler was called for initial state by Hash.init (value will be true), or due to an actual change to the hash (false).

jQuery plugin

Also included is a jQuery plugin that simplifies the use of the Hash library.

Example

// Add events before calling init to make sure they are triggered for
// initial hash value.
$('div#log').hashchange(function (e, newHash) {
    $(this).prepend('<p>New hash: <b>"' + newHash + '"</b></p>');
});
// Initialize.
$.hash.init();
$.hash.go('abc123');
// Changes hash when the anchor is clicked. Also automatically sets the
// href attribute to "#def456", unless a second argument with a false
// value is supplied.
$('a#my-anchor').hash('def456');

MIT license

This project is licensed under an MIT license.

Copyright (c) 2009-2010 Andreas Blixt [email protected]

http://www.opensource.org/licenses/mit-license.php

About

A JavaScript library that keeps track of the history of changes to the hash part in the address bar.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •