index.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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>History.js</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. State = History.getState(),
  37. $log = $('#log');
  38. // Log Initial State
  39. History.log('initial:', State.data, State.title, State.url);
  40. // Bind to State Change
  41. History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
  42. // Log the State
  43. var State = History.getState(); // Note: We are using History.getState() instead of event.state
  44. History.log('statechange:', State.data, State.title, State.url);
  45. });
  46. // Prepare Buttons
  47. var
  48. buttons = document.getElementById('buttons'),
  49. scripts = [
  50. 'History.pushState({state:1,rand:Math.random()}, "State 1", "?state=1"); // logs {state:1,rand:"some random value"}, "State 1", "?state=1"',
  51. 'History.pushState({state:2,rand:Math.random()}, "State 2", "?state=2"); // logs {state:2,rand:"some random value"}, "State 2", "?state=2"',
  52. 'History.replaceState({state:3,rand:Math.random()}, "State 3", "?state=3"); // logs {state:3,rand:"some random value"}, "State 3", "?state=3"',
  53. 'History.pushState(null, null, "?state=4"); // logs {}, "", "?state=4"',
  54. 'History.back(); // logs {state:3}, "State 3", "?state=3"',
  55. 'History.back(); // logs {state:1}, "State 1", "?state=1"',
  56. 'History.back(); // logs {}, "The page you started at", "?"',
  57. 'History.go(2); // logs {state:3}, "State 3", "?state=3"'
  58. ],
  59. buttonsHTML = ''
  60. ;
  61. // Add Buttons
  62. for ( var i=0,n=scripts.length; i<n; ++i ) {
  63. var _script = scripts[i];
  64. buttonsHTML +=
  65. '<li><button onclick=\'javascript:'+_script+'\'>'+_script+'</button></li>';
  66. }
  67. buttons.innerHTML = buttonsHTML;
  68. })(window);
  69. </script>
  70. </div>
  71. </body>
  72. </html>