index.html 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>
  7. History.js
  8. </title>
  9. </head>
  10. <body style="padding-bottom:40px">
  11. <!-- Scripts -->
  12. <script>if ( typeof window.JSON === 'undefined' ) { document.write('<script src="../scripts/uncompressed/json2.js"><\/script>'); }</script>
  13. <script src="../vendor/jquery.js"></script>
  14. <script src="../scripts/bundled/html4+html5/jquery.history.js"></script>
  15. <!-- HTML -->
  16. <div id="wrap">
  17. <!-- Intro -->
  18. <h1><a href="https://github.com/balupton/History.js">History.js</a> by <a href="http://balupton.com">Benjamin Lupton</a></h1>
  19. <p>History.js gracefully supports the <a href="https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history">HTML5 History/State APIs</a> (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState. Supports <a href="http://jquery.com/">jQuery</a>, <a href="http://mootools.net">MooTools</a> and <a href="http://prototypejs.org">Prototype</a>. For HTML5 browsers this means that you can modify the URL directly, without needing to use hashes anymore. For HTML4 browsers it will revert back to using the old onhashchange functionality.</p>
  20. <!-- Textarea for Logging -->
  21. <textarea id="log" style="width:100%;height:400px"></textarea>
  22. <!-- Note -->
  23. <p>Click through the buttons in order and you'll get the results demonstrated in the <a href="../README.md">README.md</a> file.</p>
  24. <!-- Buttons -->
  25. <ul id="buttons">
  26. </ul>
  27. <!-- Our Script -->
  28. <script>
  29. (function(window,undefined){
  30. // Check Location
  31. if ( document.location.protocol === 'file:' ) {
  32. alert('The HTML5 History API (and thus History.js) do not work on files, please upload it to a server.');
  33. }
  34. // Establish Variables
  35. var
  36. History = window.History, // Note: We are using a capital H instead of a lower h
  37. State = History.getState(),
  38. $log = $('#log');
  39. // Log Initial State
  40. History.log('initial:', State.data, State.title, State.url);
  41. // Bind to State Change
  42. History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
  43. // Log the State
  44. var State = History.getState(); // Note: We are using History.getState() instead of event.state
  45. History.log('statechange:', State.data, State.title, State.url);
  46. });
  47. // Prepare Buttons
  48. var
  49. buttons = document.getElementById('buttons'),
  50. scripts = [
  51. 'History.pushState({state:1,rand:Math.random()}, "State 1", "?state=1"); // logs {state:1,rand:"some random value"}, "State 1", "?state=1"',
  52. 'History.pushState({state:2,rand:Math.random()}, "State 2", "?state=2"); // logs {state:2,rand:"some random value"}, "State 2", "?state=2"',
  53. 'History.replaceState({state:3,rand:Math.random()}, "State 3", "?state=3"); // logs {state:3,rand:"some random value"}, "State 3", "?state=3"',
  54. 'History.pushState(null, null, "?state=4"); // logs {}, "", "?state=4"',
  55. 'History.back(); // logs {state:3}, "State 3", "?state=3"',
  56. 'History.back(); // logs {state:1}, "State 1", "?state=1"',
  57. 'History.back(); // logs {}, "The page you started at", "?"',
  58. 'History.go(2); // logs {state:3}, "State 3", "?state=3"'
  59. ],
  60. buttonsHTML = ''
  61. ;
  62. // Add Buttons
  63. for ( var i=0,n=scripts.length; i<n; ++i ) {
  64. var _script = scripts[i];
  65. buttonsHTML +=
  66. '<li><button onclick=\'javascript:'+_script+'\'>'+_script+'</button></li>';
  67. }
  68. buttons.innerHTML = buttonsHTML;
  69. })(window);
  70. </script>
  71. </div>
  72. </body>
  73. </html>