Skip to content

Instantly share code, notes, and snippets.

@millermedeiros
Created July 27, 2011 16:28
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save millermedeiros/1109742 to your computer and use it in GitHub Desktop.
Save millermedeiros/1109742 to your computer and use it in GitHub Desktop.
Using hasher together with crossroads.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>example crossroads + hasher</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="wrapper">
<ul>
<li>
<a href="#/home">home</a>
</li>
<li>
<a href="#/lorem">lorem</a>
</li>
<li>
<a href="#/lorem/ipsum">lorem/ipsum</a>
</li>
</ul>
</div>
<!--
required scripts:
* js-signals (http://millermedeiros.github.com/js-signals/)
* crossroads.js (http://millermedeiros.github.com/crossroads.js/)
* hasher (https://github.com/millermedeiros/hasher/)
* sample (https://gist.github.com/1109742)
-->
<script src="signals.js"></script>
<script src="crossroads.js"></script>
<script src="hasher.js"></script>
<script src="sample-hasher_crossroads.js"></script>
</body>
</html>
var DEFAULT_HASH = 'home';
//setup crossroads
crossroads.addRoute('home');
crossroads.addRoute('lorem');
crossroads.addRoute('lorem/ipsum');
crossroads.routed.add(console.log, console); //log all routes
//setup hasher
//only required if you want to set a default value
if(! hasher.getHash()){
hasher.setHash(DEFAULT_HASH);
}
function parseHash(newHash, oldHash){
// second parameter of crossroads.parse() is the "defaultArguments" and should be an array
// so we ignore the "oldHash" argument to avoid issues.
crossroads.parse(newHash);
}
hasher.initialized.add(parseHash); //parse initial hash
hasher.changed.add(parseHash); //parse hash changes
hasher.init(); //start listening for hash changes
@duhduhdan
Copy link

@amicns I know this was posted almost a year ago, but if anyone else is running into the same problem here's what I did:

var DEFAULT_HASH = 'home', url = hasher.getBaseURL();

if (hasher.getURL() === url) {
    hasher.setHash(DEFAULT_HASH);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment