html5history.html 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>goog.history.Html5History Demo</title>
  10. <script src="../base.js"></script>
  11. <script>
  12. goog.require('goog.events');
  13. goog.require('goog.history.EventType');
  14. goog.require('goog.history.Html5History');
  15. goog.require('goog.Uri');
  16. </script>
  17. <style>
  18. .section {
  19. display: none;
  20. }
  21. .active {
  22. display: block;
  23. }
  24. </style>
  25. <link rel="stylesheet" href="css/demo.css">
  26. </head>
  27. <body>
  28. <h1>goog.history.Html5History</h1>
  29. <p id="links">
  30. <a href="kittens/" token="kittens" title="Kittens">Kittens</a> |
  31. <a href="puppies/" token="puppies" title="Puppies">Puppies</a> |
  32. <a href="pandas/" token="pandas" title="Pandas">Pandas</a> |
  33. <a href="bucket/" token="bucket" title="Bucket">Bucket</a>
  34. </p>
  35. <div id="kittens" class="section active">
  36. <img src="http://farm4.static.flickr.com/3045/2481490798_bba0c585b4.jpg">
  37. </div>
  38. <div id="puppies" class="section">
  39. <img src="http://farm4.static.flickr.com/3178/2976942142_7f5be73d21.jpg">
  40. </div>
  41. <div id="pandas" class="section">
  42. <img src="http://farm1.static.flickr.com/106/303160549_49917ba1b7.jpg">
  43. </div>
  44. <div id="bucket" class="section">
  45. <img src="http://farm1.static.flickr.com/146/421093356_6bbefa304e.jpg">
  46. </div>
  47. <script>
  48. var h;
  49. try {
  50. h = new goog.history.Html5History();
  51. } catch (e) {
  52. document.write(e.message);
  53. }
  54. if (h) {
  55. var cur = 'kittens';
  56. goog.events.listen(h, goog.history.EventType.NAVIGATE, function(e) {
  57. var token = e.token || 'kittens';
  58. var next = document.getElementById(token);
  59. if (next) {
  60. document.getElementById(cur).className = 'section';
  61. next.className = 'section active';
  62. cur = token;
  63. }
  64. });
  65. h.setUseFragment(false);
  66. h.setPathPrefix(new goog.Uri(document.location.href).getPath() + '/');
  67. h.setEnabled(true);
  68. goog.events.listen(
  69. document.getElementById('links'), 'click', function(e) {
  70. if (e.target.tagName == 'A') {
  71. h.setToken(e.target.getAttribute('token'), e.target.title);
  72. e.preventDefault();
  73. }
  74. });
  75. }
  76. </script>
  77. </body>
  78. </html>