Created
January 24, 2017 10:54
-
-
Save agrublev/272820c9261c51a7211d1e8cf2914fad to your computer and use it in GitHub Desktop.
How to use crossroads.js with history.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<style> | |
body {background:black;color:white;} | |
</style> | |
<script src="jquery.js"></script> | |
<script src="signal.js"></script> | |
<script src="history.js"></script> | |
<script src="history.adapter.js"></script> | |
<script src="routes.js"></script> | |
<script> | |
$(function(){ | |
// Prepare | |
crossroads.addRoute('/test/{id}', function(id){ | |
console.log(id,'888888888888888'); | |
}); | |
// Bind to StateChange Event | |
History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate | |
var State = History.getState(); // Note: We are using History.getState() instead of event.state | |
console.log(State); | |
crossroads.parse(document.location.pathname + document.location.search); | |
}); | |
$("body").on("click","a[data-href]",function(e){ | |
e.preventDefault(); | |
History.pushState({state:1}, "State 1", "/test/3"); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
hi | |
<a href="#">Test</a> | |
<a href="/test/two">Test2</a> | |
<a href="#" data-href="/test/three">Test3</a> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I spend way too much not finding a clean example like this. So here you go.